iOS键盘遮挡住输入框解决办法
1.设置键盘的类型,returnKeyType =UIReturnKeyDone;(可以根据具体情况设置键盘类型)
- (BOOL)textFieldShouldReturn:(UITextField *)textField{
[self.view endEditing:YES];
return YES;
}
2.发通知(针对scrollview设置contentSize 针对tableview设置y
self.tableView.y = SCREEN_HEIGHT - self.keyBoardHeight - self.tableView.height+64;
)
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(keyBoardChange:) name:UIKeyboardDidChangeFrameNotification object:nil];
- (void)keyBoardChange:(NSNotification *)notification{
NSDictionary *dict = notification.userInfo;
NSValue *aValue = [dict objectForKey:UIKeyboardFrameEndUserInfoKey];
NSNumber *animationTime = [dict objectForKey:@"UIKeyboardAnimationDurationUserInfoKey"];
CGRect keyboardRect = [aValue CGRectValue];
CGFloat keyHeight = SCREEN_HEIGHT - keyboardRect.origin.y;
if(keyHeight==0){
[UIView animateWithDuration:[animationTime doubleValue] animations:^{
self.scroll.contentSize = CGSizeMake(0,self.scrollHeight);
} completion:^(BOOL finished) {
}];
}else{
CGRect begin = [[[notification userInfo] objectForKey:@"UIKeyboardFrameBeginUserInfoKey"] CGRectValue];
CGRect end = [[[notification userInfo] objectForKey:@"UIKeyboardFrameEndUserInfoKey"] CGRectValue];
// 第三方键盘回调三次问题,监听仅执行最后一次
if(begin.size.height>0 && (begin.origin.y-end.origin.y>0)){
[UIView animateWithDuration:[animationTime doubleValue] animations:^{
self.scroll.contentSize = CGSizeMake(0,self.scrollHeight + end.size.height);
} completion:^(BOOL finished) {
}];
}else{
[UIView animateWithDuration:[animationTime doubleValue] animations:^{
self.scroll.contentSize = CGSizeMake(0,self.scrollHeight + end.size.height);
} completion:^(BOOL finished) {
}];
}
}
}