iOS UIWebView鍵盤處理

讓UIWebView彈出鍵盤上的按鈕顯示中文    http://www.cr173.com/html/19440_1.html

 

若是你有下面的問題,此文也許會幫到你。javascript

  1. 鍵盤遮蓋了UIWebView。
  2. 如何拖動UIWebView來移除鍵盤。
  3. 鍵盤出現時UIWebView裏面的Content內容向上移動,以致聚焦的文本框超出了UIWebView的可視區域。
  4. 如何在鍵盤彈出時禁止UIWebView裏面的Content向上移動。
  5. 沒法在UIWebView中獲取到座標,來計算contentOffset獲得想要展現的結果。

+-------------------------+html

 

一步一步說明:

1. 喚出移除鍵盤

只要點擊UIWebView裏面的html文本框控件,會自動彈出鍵盤。固然你須要獲取鍵盤的信息(高度等),方法仍是使用UIViewController+Notification的方式,代碼以下:java

// 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已爲咱們提供了這些,代碼以下:web

self.webView.scrollView.keyboardDismissMode = UIScrollViewKeyboardDismissModeOnDrag; // 當拖動時移除鍵盤  

若是是iOS7如下,請參照 6 來設置,大概思路,先添加一個private的flag代表如今鍵盤是否存在,當存在時,經過 6 來獲取事件關閉鍵盤。app

3. 鍵盤遮蓋了UIWebView

這個的解決方法可在 1 中的keyboardWillShow:裏面操做,經過改變webView的origin來實現。ide

4. 鍵盤出現時UIWebView裏面的Content內容向上移動,以致聚焦的文本框超出了UIWebView的可視區域

在UIWebView中,只要鍵盤出現,UIWebView確定會向上移動,至於合不合適就很差說了,若是不合適,就只用禁用自動移動。post

5. 如何在鍵盤彈出時禁止UIWebView裏面的Content向上移動

這個方法,我也找了好久,可是仍是找到了,感謝強大的網友,代碼以下:ui

@interface XXX : UIViewController<UIScrollViewDelegate> // 添加UIScrollViewDelegate, step 1  
  
self.webView.scrollView.delegate = self; // 註冊代理, step 2  
  
- (UIView*)viewForZoomingInScrollView:(UIScrollView*)scrollView{ // 實現代理方法, step 3  
        return nil;  
}  

6. 如何在UIWebView中獲取點擊座標

衆所周知,UIWebView會吃掉全部的touch事件,否則也不會有那麼多人費工夫弄javascript了,可是不能設置不表明不能以另一種方式代替,大概思路:給webView的superView添加手勢,而後經過實現多手勢過濾設置來實現,爲何要設置多手勢過濾呢?我這裏說明一下,因爲UIWebView默認有本身的手勢,它會攔截掉你的手勢,以致 superView沒法接收手勢,代碼以下:spa

@interface XXX : UIViewController<UIGestureRecognizerDelegate> // 添加UIGestureRecognizerDelegate, step 1  
  
// 添加手勢, step 2  
UITapGestureRecognizer *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鍵盤處理能想起的就只有這些了,歡迎你們補充。代理

 

轉自:http://www.open-open.com/lib/view/open1408609311366.html

相關文章
相關標籤/搜索