1.聲明屬性(記錄將要編輯的輸入框)
@property(nonatomic ,strong) UITextField * firstResponderTextF;(這里是記錄將要編輯的輸入框)
2.//監(jiān)聽鍵盤展示和隱藏的通知
- (void)viewDidLoad {
? ? [super viewDidLoad];
? ? // Do any additional setup after loading the view.
? ? //監(jiān)聽鍵盤展示和隱藏的通知
? ? [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
? ? [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
}
3.#pragma maek UITextFieldDelegate
- (BOOL)textFieldShouldBeginEditingNew:(UITextField *)textField{
? ? self.firstResponderTextF = textField;//當(dāng)將要開始編輯的時(shí)候,獲取當(dāng)前的textField
? ? return YES;
}
- (BOOL)textFieldShouldReturnNew:(UITextField *)textField{
? ? [textFieldresignFirstResponder];
? ? return YES;
}
4.#pragma mark : UIKeyboardWillShowNotification/UIKeyboardWillHideNotification
- (void)keyboardWillShow:(NSNotification*)notification{
? ? CGRect rect = [self.firstResponderTextF.superview convertRect:self.firstResponderTextF.frame toView:self.view];//獲取相對(duì)于self.view的位置
? ? NSDictionary*userInfo = [notificationuserInfo];
? ? NSValue* aValue = [userInfo objectForKey:UIKeyboardFrameEndUserInfoKey];//獲取彈出鍵盤的fame的value值
? ? CGRectkeyboardRect = [aValueCGRectValue];
? ? keyboardRect = [self.view convertRect:keyboardRect fromView:self.view.window];//獲取鍵盤相對(duì)于self.view的frame ,傳window和傳nil是一樣的
? ? CGFloatkeyboardTop = keyboardRect.origin.y;
? ? NSNumber * animationDurationValue = [userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey];//獲取鍵盤彈出動(dòng)畫時(shí)間值
? ? NSTimeIntervalanimationDuration = [animationDurationValuedoubleValue];
? ? if(keyboardTop
? ? ? ? CGFloatgap = keyboardTop -CGRectGetMaxY(rect) -10;//計(jì)算需要網(wǎng)上移動(dòng)的偏移量(輸入框底部離鍵盤頂部為10的間距)
? ? ? ? __weaktypeof(self)weakSelf =self;
? ? ? ? [UIView animateWithDuration:animationDuration animations:^{
? ? ? ? ? ? weakSelf.view.frame=CGRectMake(weakSelf.view.frame.origin.x, gap, weakSelf.view.frame.size.width, weakSelf.view.frame.size.height);
? ? ? ? }];
? ? }
}
- (void)keyboardWillHide:(NSNotification*)notification{
? ? NSDictionary*userInfo = [notificationuserInfo];
? ? NSNumber * animationDurationValue = [userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey];//獲取鍵盤隱藏動(dòng)畫時(shí)間值
? ? NSTimeIntervalanimationDuration = [animationDurationValuedoubleValue];
? ? if (self.view.frame.origin.y < 0) {//如果有偏移嘱巾,當(dāng)影藏鍵盤的時(shí)候就復(fù)原
? ? ? ? __weaktypeof(self)weakSelf =self;
? ? ? ? [UIView animateWithDuration:animationDuration animations:^{
? ? ? ? ? ? weakSelf.view.frame = CGRectMake(weakSelf.view.frame.origin.x, 0, weakSelf.view.frame.size.width, weakSelf.view.frame.size.height);
? ? ? ? }];
? ? }
}
5.// 退出鍵盤
-(void)touchesBegan:(NSSet *)toucheswithEvent:(UIEvent*)event{
? ? if ([self.firstResponderTextF isFirstResponder])[self.firstResponderTextF resignFirstResponder];
? ? [self.view endEditing:YES];
}
6.//移除鍵盤通知監(jiān)聽者
- (void)dealloc{
? ? self.countDowntor.delegate = nil;
? ? //移除鍵盤通知監(jiān)聽者
? ? [[NSNotificationCenter defaultCenter]removeObserver:self name:UIKeyboardWillShowNotification object:nil];
? ? [[NSNotificationCenter defaultCenter]removeObserver:self name:UIKeyboardWillHideNotification object:nil];
}