@interface XXXViewController ()
@property (nonatomic,strong)UITapGestureRecognizer *keyboardTap;
@property (nonatomic, assign) NSInteger keyboardTagRow;//键盘弹出,界面上滑
@property (nonatomic, assign) NSInteger keyboardTagSection;//键盘弹出,界面上滑
@property (nonatomic, assign) CGFloat scrollFloat;//键盘弹出,界面上滑
@end
@implementation XXXViewController
-(void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[self addKeyboardNotification];//键盘监听
}
-(void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
//释放
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];
[[NSNotificationCenter defaultCenter]removeObserver:self];
}
#pragma mark -键盘监听
- (void)addKeyboardNotification
{
//增加监听,当键盘出现或改变时收出消息
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillShow:)
name:UIKeyboardWillShowNotification
object:nil];
//增加监听,当键退出时收出消息
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillHide:)
name:UIKeyboardWillHideNotification
object:nil];
_keyboardTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hidekeyBoard:)];
_keyboardTap.cancelsTouchesInView = NO;
}
//当键盘出现或改变时调用
- (void)keyboardWillShow:(NSNotification *)aNotification
{
//获取键盘的高度
NSDictionary *userInfo = [aNotification userInfo];
NSValue *aValue = [userInfo objectForKey:UIKeyboardFrameEndUserInfoKey];
CGRect keyboardRect = [aValue CGRectValue];
int keyboardHeight = keyboardRect.size.height;
NSTimeInterval animationDuration=0.30f;
NSIndexPath *indexPath=[NSIndexPath indexPathForRow:_keyboardTagRow inSection:_keyboardTagSection];
CGRect rectInTableView =[_myTableView rectForRowAtIndexPath:indexPath];
CGRect rectInSuperview = [_myTableView convertRect:rectInTableView toView:self.view];
if (keyboardHeight>(kScreenHeight-(rectInSuperview.origin.y+rectInSuperview.size.height))) {
[UIView animateWithDuration:animationDuration animations:^{
_myTableView.contentInset = UIEdgeInsetsMake(64, 0, keyboardHeight, 0);
[_myTableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionMiddle animated:YES];
}];
}
[self.view addGestureRecognizer:_keyboardTap];
}
//当键退出时调用
- (void)keyboardWillHide:(NSNotification *)aNotification{
_myTableView.contentInset = UIEdgeInsetsMake(0, 0, 0, 0);
[self.view removeGestureRecognizer:_keyboardTap];
}
#pragma mark - 点击隐藏键盘
-(void)hidekeyBoard:(UITapGestureRecognizer *)gesture{
UIView *view = gesture.view;
[self hideKeyboardImpl:view];
}
-(void) hideKeyboardImpl:(UIView*) view
{
[self findResponderAndResign:view];
}
-(BOOL) findResponderAndResign:(UIView*) view
{
if (view)
{
if ([view isFirstResponder])
{
[view resignFirstResponder];
return YES;
}
NSArray *children = [view subviews];
for (int i = 0; i < children.count; ++i)
{
UIView *child = children[i];
if ([self findResponderAndResign:child])
{
return YES;
}
}
}
return NO;
}