步驟1.viewDidLoad{}方法中設(shè)置監(jiān)聽
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(revertViewBounds) name:UIKeyboardWillHideNotification object:nil];
步驟2.在.m中添加此方法,此時(shí)已經(jīng)設(shè)置結(jié)束編輯時(shí)(收起鍵盤時(shí)頁(yè)面位置)
- (void)revertViewBounds
{
if (self.view.bounds.origin.y > 0)
{
[UIView animateWithDuration:0.2 animations:^{
self.view.bounds = CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height);
}];
}
}
步驟3.設(shè)置開始編輯時(shí)頁(yè)面位置
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
//可以在此處設(shè)置彈出鍵盤時(shí)頁(yè)面布局
CGRect newTFRect = [textField convertRect:textField.bounds toView:self.view];
CGFloat dist = (ScreenHeight - 216) - (newTFRect.origin.y + newTFRect.size.height + 100);
if (dist < 0) {
[UIView animateWithDuration:0.3 animations:^{
self.view.bounds = CGRectMake(0, -dist, self.view.bounds.size.width, self.view.bounds.size.height);
}];
}
return YES;
}