1.監(jiān)聽鍵盤出現(xiàn)、消失以更改view位置
- (void)keyboardWillShow:(NSNotification *)notification {
CGFloat keyboardHeight = [[notification.userInfo objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size.height;//獲取鍵盤高度
CGFloat screenHeight = UIScreen.mainScreen.bounds.size.height;
CGRect customHousePriceViewRect = self.customHousePriceView.frame;
CGFloat offset = customHousePriceViewRect.origin.y + self.view.frame.origin.y - (screenHeight - keyboardHeight - customHousePriceViewRect.size.height - 64);//偏移距離 64是狀態(tài)欄和導(dǎo)航欄的高度和(20 + 44)
double animateTime = [[notification.userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue];//鍵盤出現(xiàn)的動畫時間
if (offset > 0) {
[UIView animateWithDuration:animateTime animations:^{
self.customHousePriceView.frame = CGRectMake(0, customHousePriceViewRect.origin.y-offset, customHousePriceViewRect.size.width, customHousePriceViewRect.size.height);
}];
}
}
- (void)keyboardWillHide:(NSNotification *)notification {
double animateTime = [[notification.userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue];//鍵盤出現(xiàn)的動畫時間
[UIView animateWithDuration:animateTime animations:^{
[self loadFrameOfSubViews];
}];
}