在輸入文本時,鍵盤彈出,遮擋了視圖內容,此時可通過鍵盤通知來使你的UI組件向上或向移動,或對它們進行重組。
各鍵盤通知:
UIKeyboardWillShowNotification:包含user-info dictionary
UIKeyboardDidShowNotification
UIKeyboardWillHideNotification:包含user-info dictionary
UIKeyboardDidHideNotification
user-info dictionary:
UIKeyboardAnimationCurveUserInfoKey: 包含NSUInteger類型的NSNumber,封裝在NSValue中
UIKeyboardAnimationDurationUserInfoKey: 包含double類型的NSNumber,封裝在NSValue中
UIKeyboardFrameBeginUserInfoKey:CGRect類型,封裝在NSValue中,標識動畫開始之前的keyboard的frame,使用前進行座標轉換
UIKeyboardFrameEndUserInfoKey:CGRect類型,封裝在NSValue中,標識動畫開始之後的keyboard的frame,使用前進行座標轉換
e.g.
@interface ViewController () <UITextFieldDelegate>
//View裏包含ScrollView,ScrollView裏包含ImageView,TextField
@property (weak, nonatomic) IBOutlet UIScrollView *scrollView;
@property (weak, nonatomic) IBOutlet UITextField *textField; //此textField的delegate是自身ViewController
@property (weak, nonatomic) IBOutlet UIImageView *imageView;
@end
@implementation ViewController
//UITextFieldDelegte:return Button
- (BOOL) textFieldShouldReturn:(UITextField *)paramTextField{
[paramTextField resignFirstResponder];
return YES;
}
- (void) viewWillAppear:(BOOL)paramAnimated{
[super viewWillAppear:paramAnimated];
NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
//註冊通知-鍵盤彈出
[center addObserver:self selector:@selector(handleKeyboardWillShow:)
name:UIKeyboardWillShowNotification object:nil];
//註冊通知-鍵盤隱藏
[center addObserver:self selector:@selector(handleKeyboardWillHide:)
name:UIKeyboardWillHideNotification object:nil];
}
- (void)viewWillDisappear:(BOOL)paramAnimated{
[super viewWillDisappear:paramAnimated];
//取消註冊
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
- (void) handleKeyboardWillShow:(NSNotification *)paramNotification{
NSDictionary *userInfo = paramNotification.userInfo;
//獲取鍵盤彈出的動畫持續時間,以便後續我們使用相同的時間進行動畫呈現內容
NSValue *animationDurationObject = userInfo[UIKeyboardAnimationDurationUserInfoKey];
//獲取鍵盤彈出結束時的frame,是以屏幕座標形式表示,因此後續使用需要轉換成窗體座標 convertRect:fromView:
NSValue *keyboardEndRectObject = userInfo[UIKeyboardFrameEndUserInfoKey];
double animationDuration = 0.0;
CGRect keyboardEndRect = CGRectMake(0.0f, 0.0f, 0.0f, 0.0f);
[animationDurationObject getValue:&animationDuration];
[keyboardEndRectObject getValue:&keyboardEndRect];
UIWindow *window = [UIApplication sharedApplication].keyWindow;
keyboardEndRect = [self.view convertRect:keyboardEndRect fromView:window];
/* 鍵盤覆蓋的部分*/
CGRect intersectionOfKeyboardRectAndWindowRect = CGRectIntersection(self.view.frame, keyboardEndRect);
/* 滾動視圖以顯示遮擋內容*/
[UIView animateWithDuration:animationDuration animations:^{
self.scrollView.contentInset = UIEdgeInsetsMake(0.0f, //上
0.0f, //左
intersectionOfKeyboardRectAndWindowRect.size.height, //下
0.0f); //右
//滾動視圖使標識的rect剛好可見
[self.scrollView scrollRectToVisible:self.textField.frame animated:NO];
}];
}
- (void) handleKeyboardWillHide:(NSNotification *)paramSender{
NSDictionary *userInfo = [paramSender userInfo];
NSValue *animationDurationObject = [userInfo valueForKey:UIKeyboardAnimationDurationUserInfoKey];
double animationDuration = 0.0;
[animationDurationObject getValue:&animationDuration];
[UIView animateWithDuration:animationDuration animations:^{
self.scrollView.contentInset = UIEdgeInsetsZero; //0,0,0,0
}];
}