iOS UIWebView鍵盤處理

jopen 10年前發布 | 131K 次閱讀 IOS iOS開發 移動開發

如果你有下面的問題,此文也許會幫到你。

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

    +-------------------------+

     

    一步一步說明:

    1.  喚出移除鍵盤

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

    </div> </div>

        // 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同上面的方法  
        // 你的操作,如鍵盤移除,控制視圖還原等  
    }  </pre><br />
    

    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> // 添加UIScrollViewDelegate, step 1

    self.webView.scrollView.delegate = self; // 注冊代理, step 2  
    
    - (UIView*)viewForZoomingInScrollView:(UIScrollView*)scrollView{ // 實現代理方法, step 3  
            return nil;  
    }  </pre><a style="text-indent:0px;" title="派生到我的代碼片" href="/misc/goto?guid=4959613894216285742" target="_blank"></a></div>
    

    </div> </div>

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

    眾所周知,UIWebView會吃掉所有的touch事件,不然也不會有那么多人費工夫弄javascript了,但是不能設置不代表不能以另外一種方式代替,大概思路:給webView的superView添加手勢,然后通過實現多手勢過濾設置來實現,為什么要設置多手勢過濾呢?我這里說明一下,由于UIWebView默認有自己的手勢,它會攔截掉你的手勢,以至 superView無法接收手勢,代碼如下:

        @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);  
    }  </pre><a style="text-indent:0px;" title="派生到我的代碼片" href="/misc/goto?guid=4959613894216285742" target="_blank"></a></div>
    

    </div> </div>
    UIWebView鍵盤處理能想起的就只有這些了,歡迎大家補充。

    BB:轉載請注明出處 http://blog.csdn.net/assholeu/article/details/38714123

    資料參考:
    感謝 http://blog.csdn.net/abel_tu/article/details/12134261

     本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
     轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
     本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!