0

I've been pulling my hair for hours now regarding my xib popup acting weird in iOS 10 and above, this problem happens when textFieldDidBeginEditing is fired, here is video of the issue. Code for xib popup:

After a few times debugging this problem. I noticed that the textFieldDidBeginEditing not cause this weird issue. What I suspect is a MJPopupViewController which caused this issue.

#import "NewPopView.h"
#import "UIViewController+MJPopupViewController.h"

@interface NewBottlePopView() <UITextFieldDelegate>
@property (strong, nonatomic) UIViewController *viewController;
@end
@implementation NewBottlePopView
@synthesize viewController;

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
    }
    return self;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/

-(void)showPopUpInViewController:(UIViewController *)controller{

    if ([controller class] == [UINavigationController class]) {
        viewController = [(UINavigationController*)controller visibleViewController];
        [viewController setKeepOnTouchOutside:NO];
        [viewController presentPopupView:self animationType:MJPopupViewAnimationSlideBottomBottom];
    }else{
        viewController = controller;
        [viewController setKeepOnTouchOutside:NO];
        [viewController presentPopupView:self animationType:MJPopupViewAnimationSlideBottomBottom];
    }
}

- (IBAction)close:(id)sender {
    [viewController dismissPopupViewControllerWithanimationType:MJPopupViewAnimationSlideBottomBottom];
}


#pragma mark Text Field Delegate
-(void)textFieldDidBeginEditing:(UITextField *)textField {
    NSLog(@"screen width: %f", [[UIScreen mainScreen] applicationFrame].size.width);
    if ([[UIScreen mainScreen] applicationFrame].size.width <= 320)
    {
        //CGFloat newY = -textField.frame.origin.y - self.frame.size.height;
        CGFloat newY = -textField.frame.origin.y + 140;

        if (newY >= self.frame.origin.y)
            newY = self.frame.origin.y;

        [UIView animateWithDuration:0.3 animations:^{
            self.frame = CGRectMake(self.frame.origin.x,
                                    newY,
                                    self.frame.size.width,
                                    self.frame.size.height);   //resize
        }];
    }
}

-(void)textFieldDidEndEditing:(UITextField *)textField {
    if (textField.tag == 202)
    {
        NSError *error;
        NSRegularExpression *regex = [[NSRegularExpression alloc] initWithPattern:@"[^\\d]" options:NSRegularExpressionCaseInsensitive error:&error];
        textField.text = [regex stringByReplacingMatchesInString:textField.text options:0 range:NSMakeRange(0, [textField.text length]) withTemplate:@""];
    }

    textField.text = [textField.text uppercaseString];
    [textField resignFirstResponder];
//    [self tapBackground:[self.gestureRecognizers firstObject]];
}

-(BOOL)textFieldShouldReturn:(UITextField*)textField;
{
    textField.text = [textField.text uppercaseString];
    NSInteger nextTag = textField.tag + 1;
    // Try to find next responder
    UIResponder* nextResponder = [textField.superview viewWithTag:nextTag];
    if (nextResponder) {
        // Found next responder, so set it.
        [nextResponder becomeFirstResponder];
    } else {
        // Not found, so remove keyboard.
        [textField resignFirstResponder];
        [self tapBackground:[self.gestureRecognizers firstObject]];
    }
    return NO; // We do not want UITextField to insert line-breaks.
}

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    textField.text = [textField.text uppercaseString];
    return YES;
}

-(void)tapBackground:(UIGestureRecognizer*)getsure{
    [self endEditing:YES];

    [UIView animateWithDuration:0.3 animations:^{
        [self setCenter:viewController.view.center];
    }];
}

@end

I hope someone can help me with this problem as I cannot thinking anymore. Thanks in advance.

  • 1
    Why do you look for `[[UIScreen mainScreen] applicationFrame].size.width` instead of the height? You should detect if the height is not enough to have the keyboard.frame.origin.y and yourview.frame.origin.y+yourview.frame.size.y, no ? – Larme Sep 14 '17 at 09:40
  • 1
    Wow. That is the first time I have ever seen anyone use YouTube to show off the problem. Good job +1 –  Sep 14 '17 at 09:41

1 Answers1

0

Finally I found a solution for this problem by following this post :

iOS 11 - Keyboard Height is returning 0 in keyboard notification