專案總結二:解決鍵盤覆蓋tableview的問題

weixin_33728268發表於2016-09-29

一、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;

}

相關文章