How to make a UIScrollView auto scroll when a UITextField becomes a first responder

前端 未结 11 1815
有刺的猬
有刺的猬 2021-01-31 03:24

I\'ve seen posts around here that suggest that UIScrollViews should automatically scroll if a subview UITextField becomes the first responder; however,

11条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-01-31 03:41

    You can use this function for autoScroll of UITextField

    on UITextFieldDelegate

    - (void)textFieldDidBeginEditing:(UITextField *)textField {
    
    [self autoScrolTextField:textField onScrollView:self.scrollView];
    }
    
    
    
    
    - (void) autoScrolTextField: (UITextField *) textField onScrollView: (UIScrollView *) scrollView { 
     float slidePoint = 0.0f;
    float keyBoard_Y_Origin = self.view.bounds.size.height - 216.0f;
    float textFieldButtomPoint = textField.superview.frame.origin.y + (textField.frame.origin.y + textField.frame.size.height);
    
    if (keyBoard_Y_Origin < textFieldButtomPoint - scrollView.contentOffset.y) {
        slidePoint = textFieldButtomPoint - keyBoard_Y_Origin + 10.0f;
        CGPoint point = CGPointMake(0.0f, slidePoint);
        scrollView.contentOffset = point;
    }
    

    EDIT:

    Im now using IQKeyboardManager Kudos to the developer of this, you need to try this.

提交回复
热议问题