iOS UIWebView鍵盤處理
+-------------------------+
如果你有下面的問題,此文也許會幫到你。
鍵盤遮蓋了UIWebView。如何拖動UIWebView來移除鍵盤。鍵盤出現時UIWebView裡面的Content內容向上移動,以至聚焦的文字框超出了UIWebView的可視地區。如何在鍵盤彈出時禁止UIWebView裡面的Content向上移動。無法在UIWebView中擷取到座標,來計算contentOffset得到想要展示的結果。
+-------------------------+
一步一步說明:1. 喚出移除鍵盤
只要點擊UIWebView裡面的html文字框控制項,會自動彈出鍵盤。當然你需要擷取鍵盤的資訊(高度等),方法還是使用UIViewController+Notification的方式,代碼如下:
// UIKeyboardWillShowNotification和UIKeyboardWillHideNotification為鍵盤彈出或移除時iOS系統post notification的名字,這裡只需要定義self為這個通知的接收者即可。// viewWillAppear:和viewWillDisappear:大家應該都很清楚,這兩個方法分別在self loadView和removefromsuperview後執行。// 特別注意:這裡的object參數需要是nil,不然取不到鍵盤的userInfo- (void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil]; }- (void)viewWillDisappear:(BOOL)animated { [super viewWillDisappear:animated]; [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil]; [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];}- (void)keyboardWillShow:(NSNotification *)notification { NSDictionary *userInfo = [notification userInfo]; NSValue* value = [userInfo objectForKey:UIKeyboardFrameEndUserInfoKey]; CGRect keyboardRect = [value CGRectValue]; // 這裡得到了鍵盤的frame // 你的操作,如鍵盤出現,控制視圖上移等}- (void)keyboardWillHide:(NSNotification *)notification { // 擷取info同上面的方法 // 你的操作,如鍵盤移除,控制視圖還原等}
2. 通過拖動UIWebView來移除鍵盤
在網上看見很多人為了實現這個功能做了很多操作,但在iOS7中apple已為我們提供了這些,代碼如下:
self.webView.scrollView.keyboardDismissMode = UIScrollViewKeyboardDismissModeOnDrag; // 當拖動時移除鍵盤
如果是iOS7以下,請參照 6 來設定,大概思路,先添加一個private的flag表明現在鍵盤是否存在,當存在時,通過 6 來擷取事件關閉鍵盤。
3. 鍵盤遮蓋了UIWebView這個的解決方案可在 1 中的keyboardWillShow:裡面操作,通過改變webView的origin來實現。4. 鍵盤出現時UIWebView裡面的Content內容向上移動,以至聚焦的文字框超出了UIWebView的可視地區
在UIWebView中,只要鍵盤出現,UIWebView肯定會向上移動,至於合不合適就不好說了,如果不合適,就只用禁用自動移動。
5. 如何在鍵盤彈出時禁止UIWebView裡面的Content向上移動
這個方法,我也找了很久,但是還是找到了,感謝強大的網友,代碼如下:
@interface XXX : UIViewController // 添加UIScrollViewDelegate, step 1self.webView.scrollView.delegate = self; // 註冊代理, step 2- (UIView*)viewForZoomingInScrollView:(UIScrollView*)scrollView{ // 實現代理方法, step 3 return nil;}
6. 如何在UIWebView中擷取點擊座標
眾所周知,UIWebView會吃掉所有的touch事件,不然也不會有那麼多人費工夫弄javascript了,但是不能設定不代表不能以另外一種方式代替,大概思路:給webView的superView添加手勢,然後通過實現多手勢過濾設定來實現,為什麼要設定多手勢過濾呢?我這裡說明一下,由於UIWebView預設有自己的手勢,它會攔截掉你的手勢,以至superView無法接收手勢,代碼如下:
@interface XXX : UIViewController // 添加UIGestureRecognizerDelegate, step 1// 添加手勢, step 2UITapGestureRecognizer *webTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(webTap:)];webTap.numberOfTouchesRequired = 1;webTap.numberOfTapsRequired = 1;webTap.delegate = self;webTap.cancelsTouchesInView = NO;[self.view addGestureRecognizer:webTap];// 設定過濾,ruturn YES為同時接收,至此手勢可以透過webView,讓你的superView也可以接收到了, step 3-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer{ return YES;}- (void)webTap:(UITapGestureRecognizer *)sender{ CGPoint tapPoint = [sender locationInView:self.webView.scrollView]; // 擷取相對於webView中的座標,如果改成self.view則擷取相對於superView中的座標, step 4 NSLog(@"tapPoint x:%f y:%f",tapPoint.x,tapPoint.y);}
UIWebView鍵盤處理能想起的就只有這些了,歡迎大家補充。
BB:轉載請註明出處 http://blog.csdn.net/assholeu/article/details/38714123
資料參考:感謝 http://blog.csdn.net/abel_tu/article/details/12134261