【问题标题】:How to scroll view when keyboard is appear in objective c?当键盘出现在目标c中时如何滚动视图?
【发布时间】:2012-02-18 15:23:11
【问题描述】:

我试过了,但视图太小,无法向上滚动。如何滚动更多?

// Call this method somewhere in your view controller setup code.
- (void)registerForKeyboardNotifications
{
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardWasShown:)
                                                 name:UIKeyboardDidShowNotification object:nil];

    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardWillBeHidden:)
                                                 name:UIKeyboardWillHideNotification object:nil];

}

// Called when the UIKeyboardDidShowNotification is sent.
- (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);
scroll.contentInset = contentInsets;
scroll.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, EPostaText.frame.origin) ) {
    CGPoint scrollPoint = CGPointMake(0.0, EPostaText.frame.origin.y-(aRect.size.height));
    [scroll setContentOffset:scrollPoint animated:YES];
}
}

// Called when the UIKeyboardWillHideNotification is sent
- (void)keyboardWillBeHidden:(NSNotification*)aNotification
{
UIEdgeInsets contentInsets = UIEdgeInsetsZero;
scroll.contentInset = contentInsets;
scroll.scrollIndicatorInsets = contentInsets;
}

- (void)textFieldDidBeginEditing:(UITextField *)textField
{
EPostaText = textField;

}

- (void)textFieldDidEndEditing:(UITextField *)textField
{
EPostaText = nil;
}

【问题讨论】:

标签: objective-c ios keyboard scroll


【解决方案1】:

最好的方法 - 将滚动视图容器调整为可见区域并使用它:

- (void)scrollRectToVisible:(CGRect)rect animated:(BOOL)animated

【讨论】:

    【解决方案2】:

    既然找到了,我就用TPKeyboardAvoiding

    它工作得很好,而且很容易设置:

    1. 将 UIScrollView 添加到视图控制器的 xib 中
    2. 将滚动视图的类设置为 TPKeyboardAvoidingScrollView(仍然 在 xib 中,通过身份检查器)
    3. 将所有控件放在该滚动视图中

    祝你好运!

    【讨论】:

      【解决方案3】:

      这是我的代码,希望对你有所帮助。如果您有很多文本字段,它可以正常工作

      CGPoint contentOffset;
      bool isScroll;
      - (void)textFieldDidBeginEditing:(UITextField *)textField {
          contentOffset = self.myScroll.contentOffset;
          CGPoint newOffset;
          newOffset.x = contentOffset.x;
          newOffset.y = contentOffset.y;
          //check push return in keyboar
          if(!isScroll){
              //180 is height of keyboar
              newOffset.y += 180;
              isScroll=YES;
          }
         [self.myScroll setContentOffset:newOffset animated:YES];
      }
      
      - (BOOL)textFieldShouldReturn:(UITextField *)textField{
          //reset offset of content
          isScroll = NO;
          [self.myScroll setContentOffset:contentOffset animated:YES];
          [textField endEditing:true];
          return  true;
      }
      

      【讨论】:

        猜你喜欢
        • 2011-09-02
        • 2012-02-01
        • 1970-01-01
        • 2012-06-29
        • 1970-01-01
        • 1970-01-01
        • 2014-11-10
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多