【问题标题】:IOS Managing the Keyboard - Move UIScrollView Content upIOS 管理键盘 - 上移 UIScrollView 内容
【发布时间】:2011-10-19 17:54:18
【问题描述】:

我正在使用 IOS 开发人员库中的示例来管理键盘。 Text, Web and Edition Programming Guide

我使用 IB 创建了一个视图。它是一个简单的 UI,具有 UIScrollView、UITextView、UIButton 和 UITextField。我将 UIScrollView 放在我的视图上,然后将所有其他控件添加为该滚动视图的子项。滚动视图通过 IBOutlet“scrollView”暴露给视图控制器。

以下代码在用户将焦点设置到 textField 时执行,但我从未看到滚动条出现并且滚动条的内容没有移动。我应该能够默认看到滚动条吗?谁能告诉我我错过了什么?

  -(void) keyboardWasShown:(NSNotification *)aNotification{

    NSDictionary * info = [aNotification userInfo];
    CGSize kbSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;

    UIEdgeInsets contentInsets = UIEdgeInsetsMake(0.0, 0.0, kbSize.height, 0.0);
    scrollView.contentInset = contentInsets;
    scrollView.scrollIndicatorInsets = contentInsets;

    // If active text field is hidden by keyboard, scroll it so it's visible
    // Your application might not need or want this behavior.
    CGRect aRect = self.view.frame;
    aRect.size.height -= kbSize.height;
    if (!CGRectContainsPoint(aRect, activeField.frame.origin) ) {
        CGPoint scrollPoint = CGPointMake(0.0, activeField.frame.origin.y-kbSize.height);
        [scrollView setContentOffset:scrollPoint animated:YES];
    }
}

同样,我直接从链接中的 IOS 编程指南中获取此代码。 UI 布局看起来像一个基本的聊天窗口。我想在软键盘可见时向上移动“输入”字段。谢谢! 更新

看来我需要添加一些填充才能真正看到位于滚动视图底部的控件。

 CGPoint scrollPoint = CGPointMake(0.0, (activeField.frame.origin.y - kbSize.height) + 10.0);

我怎么看不到滚动条?

【问题讨论】:

  • 您的问题标题表明您在问“如何向上移动滚动内容?”而您的问题实际上是“为什么滚动条指示器不可见?”或“如何使滚动条指示器可见?”。您可能需要对其进行编辑。

标签: objective-c ios cocoa-touch uiscrollview


【解决方案1】:

如果您有父视图作为滚动,那么只需使用:UITextFielddelegate 并设置方法

-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
    if(textField == self.txtUserName)
    {
       [self.txtPassword becomeFirstResponder];
    } 
    else if(textField == self.txtPassword){
       [self.txtPassword resignFirstResponder];
        CGPoint bottomOffset = CGPointMake(0, 0);
       [scrView setContentOffset:bottomOffset animated:YES];
       [textField resignFirstResponder];
   }
    return YES;
}

-(void)textFieldDidBeginEditing:(UITextField *)textField
{
   if (textField == self.txtUserName)
   {
       CGPoint bottomOffset = CGPointMake(0, 80);
       [scrView setContentOffset:bottomOffset animated:YES];
   }
   if (textField == self.txtPassword)
   {
      CGPoint bottomOffset = CGPointMake(0, 135);
     [scrView setContentOffset:bottomOffset animated:YES];
  }

}

【讨论】:

    【解决方案2】:

    滚动条应该只在用户交互时显示。这里不是这种情况,因为您以编程方式设置滚动视图的插图。

    如果你想显示滚动条,我相信UIScrollView 定义了一个flashScrollIndicators 方法,应该显示滚动条。

    【讨论】:

      猜你喜欢
      • 2016-05-31
      • 1970-01-01
      • 1970-01-01
      • 2014-12-09
      • 2012-01-25
      • 2015-02-15
      • 1970-01-01
      • 2014-08-17
      • 1970-01-01
      相关资源
      最近更新 更多