+-------------------------+javascript
假設你有如下的問題,也許這篇文章將幫助你。html
+-------------------------+java
僅僅要點擊UIWebView裏面的html文本框控件,會本身主動彈出鍵盤。web
固然你需要獲取鍵盤的信息(高度等),方法仍是使用UIViewController+Notification的方式,代碼例如如下:app
// 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同上面的方法 // 你的操做,如鍵盤移除。控制視圖還原等 }
在網上看見很是多人爲了實現這個功能作了很是多操做。但在iOS7中apple已爲咱們提供了這些。代碼例如如下:ssh
self.webView.scrollView.keyboardDismissMode = UIScrollViewKeyboardDismissModeOnDrag; // 當拖動時移除鍵盤
假設是iOS7下面,請參照 6 來設置,大概思路,先加入一個private的flag代表現在鍵盤是否存在,當存在時,經過 6 來獲取事件關閉鍵盤。ide
在UIWebView中,僅僅要鍵盤出現。UIWebView確定會向上移動,至於合不合適就很差說了,假設不合適。就僅僅用禁用本身主動移動。post
這種方法。我也找了很是久。但是仍是找到了,感謝強大的網友,代碼例如如下:ui
@interface XXX : UIViewController<UIScrollViewDelegate> // 加入UIScrollViewDelegate, step 1 self.webView.scrollView.delegate = self; // 註冊代理。 step 2 - (UIView*)viewForZoomingInScrollView:(UIScrollView*)scrollView{ // 實現代理方法, step 3 return nil; }
衆所周知,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); }
BB:轉載請註明出處 http://blog.csdn.net/assholeu/article/details/38714123