我的 XIB 弹出窗口在 iOS 10 及更高版本上表现得很奇怪
My XIB popup is acting weird on iOS 10 and above
关于我的 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
我想不通了,希望有人能帮我解决这个问题。提前致谢。
最后我按照这个 post 找到了解决这个问题的方法:
关于我的 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
我想不通了,希望有人能帮我解决这个问题。提前致谢。
最后我按照这个 post 找到了解决这个问题的方法: