项目总结二:解决键盘覆盖tableview的问题

一、textfield 键盘监听

1、textfiled设置代理

2、设置监听,添加通知

-(void)initNotiy{

[[NSNotificationCenterdefaultCenter]addObserver:self

selector:@selector(keyboardWillBeHidden1:)

name:UIKeyboardWillHideNotificationobject:nil];

}

#pragma mark-notif

-(void)keyboardWillBeHidden1:(NSNotification*)aNotification

{[self.viewendEditing:YES];

_addTableView.contentInset=UIEdgeInsetsMake(0,0,0,0);

}

-(void)dealloc{

[[NSNotificationCenterdefaultCenter]removeObserver:self];

}

- (BOOL)textFieldShouldBeginEditing:(UITextField*)textField

{

UIView*view = textField.superview;

while(![viewisKindOfClass:[UITableViewCellclass]]) {

view = [viewsuperview];

}

UITableViewCell*cell = (UITableViewCell*)view;

CGRectrect = [cellconvertRect:cell.frametoView:self.view];

if(rect.origin.y+ rect.size.height>=ScreenHeight-216) {

_addTableView.contentInset=UIEdgeInsetsMake(0,0,216,0);

//移动到底部[self.addTableViewscrollToRowAtIndexPath:[NSIndexPathindexPathForRow:self.listAry.count-1inSection:0]atScrollPosition:UITableViewScrollPositionBottomanimated:NO];}

return YES;

}

键盘关闭

- (BOOL)textFieldShouldReturn:(UITextField*)textField

{

return[textFieldresignFirstResponder];//需要设置代理

}


二、textView键盘监听


1.设置代理,

2.设置监听

3.代理方法操作

4.关闭键盘,这里没有返回的代理方法,需要间接的使用另一个方法

-(BOOL)textView:(UITextView*)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString*)text

{

if([textisEqualToString:@"\n"]) {

//换个思路,textview有换行,这里监听这个

[textViewresignFirstResponder];

returnNO;

}

return YES;

}

你可能感兴趣的:(项目总结二:解决键盘覆盖tableview的问题)