Home > Mobile >  Keyboard avoidance worked for UITexField - Not now in iOS 14
Keyboard avoidance worked for UITexField - Not now in iOS 14

Time:04-24

On an existing project with many many subclassed UITextFields, UITextViews and other text-based input subclasses move a UIScroll (self.view) view up nicely for keyboard avoidance.

Since iOS 14 UITextField does not move the UIScrollView up for keyboard avoidance, everything else inc UITextView, continues to work fine however UITextField does not. No changes to code or delegates have been made

   - (void)scrollToField:(id)field
{
  
    
        if (field != nil && [field isKindOfClass:[UIView class]]) {
           
            UIView *control = (UIView *)field;
            CGFloat y = control.frame.origin.y - 90.0f;
            [self.view setContentOffset:CGPointMake(0, (y < 0 ? 0 : y)) animated:YES];
    
        }
    }

The issue appears to be UIView *control = (UIView *)field;. I take it UITextField is no longer a class of UIView? On this assumption [UIView class] & UIView *control = (UIView *)field; were all changed to UITextField. Still the view does not move up as before.

   - (void)scrollToField:(id)field
{
  
    
        if (field != nil && [field isKindOfClass:[UITextField class]]) {
           
            UITextField *control = (UITextField *)field;
            CGFloat y = control.frame.origin.y - 90.0f;
            [self.view setContentOffset:CGPointMake(0, (y < 0 ? 0 : y)) animated:YES];
    
        }
    }

In summary, the questions are:

What is UITextField a class of in iOS 14?

How can I restore the functionality of moving my scrollview for UITextField prior to iOS 14

CodePudding user response:

In case anyone else has this, an offset of -90.0f placed the UITextfield slightly under the navigation bar which prevented the scrolling. Simply adjusting this to -100.0f gives a gap between the UITextField and Nav Bar and allows the scrolling behaviour as before

  • Related