一般来说,键盘遮挡主要有这么几种情况,一个是遮住UITextView,还有就是遮住UITextField,一 般来说,比较推荐在UIScrollView或者UITableView里加入textfield的控件。但是有时也许难免。
在UITextView中
这个在苹果官方文档中的项目中给出了做法,首先是注册观察者监听 UIKeyboardWillShow和WillHide事件
- (void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil]; }
实现自定义的方法
- (void)keyboardWillShow:(NSNotification *)aNotification { NSDictionary *userInfo = [aNotification userInfo]; CGRect keyboardRect = [[userInfo objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue]; NSTimeInterval animationDuration = [[userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue]; CGRect newFrame = self.view.frame; newFrame.size.height -= keyboardRect.size.height; [UIView beginAnimations:@"ResizeTextView" context:nil]; [UIView setAnimationDuration:animationDuration]; self.view.frame = newFrame; [UIView commitAnimations]; }
获取键盘显示的信息,然后根据信息对view的frame进行调整,然后WillHide方法就跟上面相 同,只不过把高度新高度改成+= keyboardRect.size.height就可以了,最后,移除观察者:
- (void)viewDidDisappear:(BOOL)animated { [super viewDidDisappear:animated]; [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil]; [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil]; }
以上是小编为您精心准备的的内容,在的博客、问答、公众号、人物、课程等栏目也有的相关内容,欢迎继续使用右上角搜索按钮进行搜索uitextview
, object
, uiview
, keyboard
, uitextfield
, ios ui uiview
, uiscrollview
, selector
, nsnotification
, 观察者
, uiscrollview尺寸
, Self
, uiscrollview详解
animated
smart keyboard输入法、input被输入法遮盖、手机输入法遮盖input、appcan 输入法遮盖、输入法弹出遮盖输入框,以便于您获取更多的相关知识。