当我们使用TextFiled或者TextView的时候,经常会因为键盘弹起而挡住编辑区域,而中文键盘因为拼音的缘故会两次调用keyboardWillShow的监听,现在我贴出一个我自己理解的方式
- <span style="font-size:18px;">
- - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
- {
- self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
- if (self) {
- // Custom initialization
- //加入监听
- [[NSNotificationCenter defaultCenter] addObserver:self
- selector:@selector(keyboardWillShow:)
- name:UIKeyboardWillShowNotification
- object:nil];
- [[NSNotificationCenter defaultCenter] addObserver:self
- selector:@selector(keyboardWillHide:)
- name:UIKeyboardWillHideNotification
- object:nil];
- }
- return self;
- }</span>
- <span style="font-size:18px;">
- @property (nonatomic,assign)CGRect tableViewRect;//设置一个rect来保存你想要升高的view的rect</span>
- <span style="font-size:18px;">- (void)keyboardWillShow:(NSNotification *)notif {
- self.keyBoardRect = [[notif.userInfo objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
- [UIView animateWithDuration:0.5 animations:^{
- CGRect tbRect = self.tableViewRect;
- tbRect.origin.y -=self.keyBoardRect.size.height;
- self.tableView.frame = tbRect;
- } completion:^(BOOL finish){
- }];
- }
- - (void)keyboardWillHide:(NSNotification *)notif {
- [UIView animateWithDuration:0.5 animations:^{
- self.tableView.frame = self.tableViewRect;
- } completion:^(BOOL finish){
- }];
- }</span>