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

前端 未结 11 1798
有刺的猬
有刺的猬 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:58

    As Michael McGuire mentioned in his point #2 above, the system's default behavior misbehaves when the scroll view contains another scroll view between the text field and the scroll view. I've found that the misbehavior also occurs when there's a scroll view merely next to the text field (both embedded in the scroll view that needs to be adjusted to bring the text field into view when the text field wants to start editing. This is on iOS 12.1.

    But my solution is different from the above. In my top-level scroll view, which is sub-classed so I can add properties and override methods, I override scrollRectToVisible:animated:. It simply calls its [super scrollRectToVisible:animated:] unless there's a property set that tells it to adjust the rect passed in, which is the frame of the text field. When the property is non-nil, it is a reference to the UITextField in question, and the rect is adjusted so that the scroll view goes further than the system thought it would. So I put this in the UIScrollView's sub-classed header file:

    @property (nullable) UITextField *textFieldToBringIntoView;
    

    (with appropriate @synthesize textFieldToBringIntoView; in the implementation. Then I added this override method to the implementation:

    - (void)scrollRectToVisible:(CGRect)rect animated:(BOOL)how
    {
        if (textFieldToBringIntoView) {
           // Do whatever mucking with `rect`'s origin needed to make it visible
           // based on context or its spatial relationship with the other
           // view that the system is getting confused by.
    
           textFieldToBringIntoView = nil;        // Go back to normal
           }
        [super scrollRectToVisible:rect animated:how];
    }
    

    In the delegate method for the UITextField for when it's about to begin editing, just set textFieldToBringIntoView to the textField in question:

    - (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
    {
        // Ensure it scrolls into view so that keyboard doesn't obscure it
        // The system is about to call |scrollRectIntoView:| for the scrolling
        // superview, but the system doesn't get things right in certain cases.
    
        UIScrollView *parent = (UIScrollView *)textField.superview;
        // (or figure out the parent UIScrollView some other way)
    
        // Tell the override to do something special just once
        // based on this text field's position in its parent's scroll view.
        parent.textFieldToBringIntoView = textField;
        // The override function will set this back to nil
    
        return(YES);
    }
    

    It seems to work. And if Apple fixes their bug, it seems like it might still work (fingers crossed).

提交回复
热议问题