引言
场景:
- 使用IQKeyboardManager 键盘管理工具,布局采用Masonry
- 在cell上使用UITextView
- textV的父控件在willMoveToWindow让textV 获取焦点导致
- 问题:键盘隐藏之后,UITableView 界面整体上移问题
- 解决方案
不要在willMoveToWindow让textV 获取焦点(推荐)
I 原因分析
- textV的父控件在willMoveToWindow让textV 获取焦点导致IQKeyboardManager是TableView 界面上移
- (void)willMoveToWindow:(UIWindow *)newWindow{ [super willMoveToWindow:newWindow]; if(self.tvModel.isDont_becomeFirstResponder){ }else{ [self.textV becomeFirstResponder]; } }
II 解决使UITableView 界面上移问题
解决方案1: 不要在willMoveToWindow让textV 获取焦点(推荐
)
因为textV的父控件在willMoveToWindow让textV 获取焦点导致IQKeyboardManager是TableView 界面上移
如果需要进入视图becomeFirstResponder时,可等视图显示完毕之后,手动调用
- (void)becomeFirstResponder{ [self.textF becomeFirstResponder]; }
解决方案2: 禁止键盘出现时的界面滚动
#pragma mark - ******** 禁止键盘出现时的界面滚动 - (void)viewWillDisappear:(BOOL)animated{ [super viewWillDisappear:animated]; [IQKeyboardManager sharedManager].enable = YES; } - (void)viewWillAppear:(BOOL)animated{ [super viewWillAppear:animated]; [HWNavigationController setupDetailnavigationItemAndBarStyle:self]; [IQKeyboardManager sharedManager].enable = NO; }
解决方案3: 置位View的transform
- 键盘隐藏的时候,恢复视图上移的高度
- (void)viewDidLoad { [super viewDidLoad]; // [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidHide:) name:UIKeyboardDidHideNotification object:nil]; self.title = @"风险处理"; [self vcView]; // } - (void)dealloc{ //[self.netReachability stopNotifier]; [[NSNotificationCenter defaultCenter] removeObserver:self]; } - (void)keyboardDidHide: (NSNotification *) notification{ NSLog(@"%@",notification.userInfo); CGFloat keyBoardAnimationDuration = [notification.userInfo[UIKeyboardAnimationDurationUserInfoKey] floatValue]; [self.view.window setBackgroundColor:self.vcView.tableView.backgroundColor]; // 解决 CGFloat y = [[UIApplication sharedApplication] statusBarFrame].size.height + self.navigationController.navigationBar.frame.size.height;// navigationBarAreaHeight //———————————————— //版权声明:本文为CSDN博主「#公众号:iOS逆向」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。 //原文链接:https://blog.csdn.net/z929118967/article/details/104639264 [UIView animateWithDuration:keyBoardAnimationDuration animations:^{ [self.view setTransform:CGAffineTransformMakeTranslation(0, y)];//设置平移的x、y值 }]; // self.view.transform = CGAffineTransformIdentity;//对设置量进行还原 }