【问题标题】:Scroll UItextfield when keyboard shows up键盘出现时滚动 UItextfield
【发布时间】:2017-08-09 00:03:15
【问题描述】:

我有一个UIView,它作为子视图添加到UIScrollViewUIView 包含UITextFieldUITextView,以避免键盘隐藏我为keyboardWasShown 和@987654328 注册的字段@通知,我写了这段代码

- (void)keyboardWasShown:(NSNotification *)notification
{
    isKeyboardUp = YES;

    // Step 1: Get the size of the keyboard.
    CGSize keyboardSize = [[[notification userInfo] objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;

    // Step 2: Adjust the bottom content inset of your scroll view by the keyboard height.
    UIEdgeInsets contentInsets = UIEdgeInsetsMake(0.0, 0.0, keyboardSize.height, 0.0);
    self.scrollView.contentInset = contentInsets;
    self.scrollView.scrollIndicatorInsets = contentInsets;

    CGRect TextFieldFrame;
    if(activeTextField)
    {
        TextFieldFrame = [activeTextField.superview convertRect:activeTextField.frame toView:self.view];
    }
    else
    {
        TextFieldFrame = [activeTextView.superview convertRect:activeTextView.frame toView:self.view];
    }

   [self.scrollView scrollRectToVisible:TextFieldFrame animated:YES];
}

- (void) keyboardWillHide:(NSNotification *)notification
{
    isKeyboardUp = NO;
    UIEdgeInsets contentInsets = UIEdgeInsetsZero;
    self.scrollView.contentInset = contentInsets;
    self.scrollView.scrollIndicatorInsets = contentInsets;
}

这里的activeTextFieldactiveTextView 是当前使用的文本字段,这在大多数情况下都很好用,但在少数情况下会失败,如果实施有什么问题,有人可以提出建议。

【问题讨论】:

标签: ios objective-c uiscrollview keyboard uitextfield


【解决方案1】:

是的,如果您使用的是滚动视图,那么您可以简单地使用TPKEYBOARDTYPING 它可以在可可豆荚上使用,或者您可以从 github 它自己获取它。 添加其文件,然后只需转到您的故事板并在滚动视图中将其类更改为 tpkeyboardtypingscrollview。

它会在返回时提供所有必要的东西,比如 Resign 键盘,并会改变 scrollView 本身的偏移量,这样你的 textFields 就不会隐藏在你的键盘后面。

【讨论】:

    【解决方案2】:

    试试这个

    -(void)keyboardWillShow:(NSNotification*)notification {
    
    NSDictionary *info = [notification userInfo];
    CGSize kbSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
    keyboardHeight = kbSize.height;
    [self updateScrollViewPosition];
    }
    
     -(void)keyboardDidChange:(NSNotification *)notification {
    
    NSDictionary *info = [notification userInfo];
    CGSize kbSizeBegin = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
    CGSize kbSizeEnd = [[info objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size;
    if (kbSizeBegin.height != kbSizeEnd.height) {
        keyboardHeight = kbSizeEnd.height;
        if (activeTextField && [activeTextField isFirstResponder]) {
            [self updateScrollViewPosition];
        }
    }
    }
    
    -(void)keyboardWillHide:(NSNotification*)notification {
    
    keyboardHeight = 0;
    activeTextField = nil;
    [self resignAllTextFields];
    } 
    
    #pragma mark - UITextFieldDelegate Methods
    
     - (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
    activeTextField = textField;
    return YES;
    }
    
    - (void)textFieldDidBeginEditing:(UITextField *)textField {
    
    activeTextField = textField;
    [self updateScrollViewPosition];
    }
    
    - (BOOL)textFieldShouldReturn:(UITextField *)textField {
    
    keyboardHeight = 0;
    activeTextField = nil;
    [textField resignFirstResponder];
    return YES;
     }
    
    #pragma mark - Update Method
    
    -(void)updateScrollViewPosition {
    
    if (keyboardHeight > 0 && activeTextField) {
        CGRect frame = activeTextField.frame;
        CGFloat yPoint = scrollView.frame.origin.y+frame.origin.y+frame.size.height+8.0;
        CGFloat height = self.view.frame.size.height-keyboardHeight;
        CGFloat diff = yPoint-height;
        if (diff > 0.0) {
            [scrollView setContentOffset:CGPointMake(0, diff) animated:YES];
        }
        else {
            CGFloat diff = scrollView.contentSize.height-scrollView.contentOffset.y;
            if (diff<scrollView.frame.size.height) {
                diff = scrollView.contentSize.height-scrollView.frame.size.height;
                if (diff < 0) {
                    diff = 0.0;
                }
                [scrollView setContentOffset:CGPointMake(0, diff) animated:YES];
            }
        }
    }
    else {
        CGFloat diff = scrollView.contentSize.height-scrollView.contentOffset.y;
        if (diff<scrollView.frame.si  ze.height) {
            diff = scrollView.contentSize.height-scrollView.frame.size.height;
            if (diff < 0) {
                diff = 0.0;
            }
            [scrollView setContentOffset:CGPointMake(0, diff) animated:YES];
        }
    }
     }
    

    辞职

    -(void)resignAllTextFields {
    
    for (UIView *view in containerView.subviews) {
        if ([view isKindOfClass:[UITextField class]]) {
            UITextField *textField = (UITextField*)view;
            [textField resignFirstResponder];
        }
    }
    [self updateScrollViewPosition];
    }
    

    【讨论】:

      猜你喜欢
      • 2010-12-19
      • 1970-01-01
      • 2015-02-02
      • 2013-02-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多