一、監(jiān)聽鍵盤出行和消失
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardHideOrShow:) name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardHideOrShow:) name:UIKeyboardWillHideNotification object:nil];
- (void)keyboardHideOrShow:(NSNotification *)notification {
NSString *notificationName = notification.name; //獲取通知名稱
NSDictionary *keyboardInfo = notification.userInfo;//獲取通知內(nèi)容
/*
NSLog(@"keyboardInfo: %@", keyboardInfo);
鍵盤將要彈起時(shí)打印 keyboardInfo: {
UIKeyboardAnimationCurveUserInfoKey = 7;
UIKeyboardAnimationDurationUserInfoKey = "0.25";
UIKeyboardBoundsUserInfoKey = "NSRect: {{0, 0}, {414, 270}}";
UIKeyboardCenterBeginUserInfoKey = "NSPoint: {207, 871}";
UIKeyboardCenterEndUserInfoKey = "NSPoint: {207, 827}";
UIKeyboardFrameBeginUserInfoKey = "NSRect: {{0, 736}, {414, 270}}";
UIKeyboardFrameEndUserInfoKey = "NSRect: {{0, 692}, {414, 270}}";
UIKeyboardIsLocalUserInfoKey = 1;
}
鍵盤將要消失時(shí)打印 keyboardInfo: {
UIKeyboardAnimationCurveUserInfoKey = 7;
UIKeyboardAnimationDurationUserInfoKey = "0.25";
UIKeyboardBoundsUserInfoKey = "NSRect: {{0, 0}, {414, 270}}";
UIKeyboardCenterBeginUserInfoKey = "NSPoint: {207, 827}";
UIKeyboardCenterEndUserInfoKey = "NSPoint: {207, 871}";
UIKeyboardFrameBeginUserInfoKey = "NSRect: {{0, 692}, {414, 270}}";
UIKeyboardFrameEndUserInfoKey = "NSRect: {{0, 736}, {414, 270}}";
UIKeyboardIsLocalUserInfoKey = 1;
}
*/
CGRect keyboardFrame = [keyboardInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
//keyboardFrame.size.height 為鍵盤的高度
if ([notificationName isEqualToString:UIKeyboardWillHideNotification]) {
//鍵盤將要消失
}
else {
//鍵盤將要彈起
}
}
二、禁止UIWebView隨鍵盤的彈起而往上滾動 借鑒于此,謝謝
問題:當(dāng)UIWebView中的html有輸入框,點(diǎn)擊輸入框,UIWebView會隨鍵盤的彈起而整體往上移動,收起鍵盤后,UIWebView無法回到原來的位置;
問題的原因:由于UIWebView繼承的是UIScrollerView,因此,當(dāng)鍵盤彈起時(shí),UIScrollerView的content會整體往上移動;
解決問題的方案:
<UIScrollViewDelegate>
_webView.scrollView.delegate = self;
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {
return nil;
}