Afiq Hamdan Afiq Hamdan - 1 year ago 73
Objective-C Question

My XIB popup is acting weird on iOS 10 and above

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.

Answer Source

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

iOS 11 - Keyboard Height is returning 0 in keyboard notification

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download