【问题标题】:My XIB popup is acting weird on iOS 10 and above我的 XIB 弹出窗口在 iOS 10 及更高版本上表现得很奇怪
【发布时间】:2017-10-03 14:15:31
【问题描述】:

我已经为我的 xib 弹出窗口在 iOS 10 及更高版本中的行为怪异而烦恼了好几个小时,这个问题发生在 textFieldDidBeginEditing 被触发时,here is video of the issue。 xib弹窗代码:

经过几次调试这个问题。我注意到 textFieldDidBeginEditing 不会导致这个奇怪的问题。我怀疑是导致此问题的 MJPopupViewController。

#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

我希望有人可以帮助我解决这个问题,因为我无法再思考了。提前致谢。

【问题讨论】:

  • 为什么要找[[UIScreen mainScreen] applicationFrame].size.width而不是身高?你应该检测是否高度不够有keyboard.frame.origin.y和yourview.frame.origin.y+yourview.frame.size.y,没有?
  • 哇。那是我第一次看到有人使用 YouTube 来炫耀这个问题。干得好+1

标签: ios objective-c ios10


【解决方案1】:

最后我通过这篇文章找到了解决这个问题的方法:

iOS 11 - Keyboard Height is returning 0 in keyboard notification

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-14
    • 1970-01-01
    • 2019-10-14
    • 1970-01-01
    • 2013-02-12
    相关资源
    最近更新 更多