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

前端 未结 11 1814
有刺的猬
有刺的猬 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条回答
  •  心在旅途
    2021-01-31 03:34

    I hope this example will help you You can scroll to any point by this code.

    scrollView.contentOffset = CGPointMake(0,0);
    

    So if you have textfield, it must have some x,y position on view, so you can use

    CGPoint point = textfield.frame.origin ;
    scrollView.contentOffset = point 
    

    This should do the trick,

    But if you don't know when to call this code, so you should learn UITextFieldDelegate methods

    Implement this method in your code

    - (void)textFieldDidBeginEditing:(UITextField *)textField {
    // Place Scroll Code here
    }
    

    I hope you know how to use delegate methods.

提交回复
热议问题