iOS UIWebView鍵盤處理,iosuiwebview鍵盤
+-------------------------+
如果你有下面的問題,此文也許會幫到你。
+-------------------------+
一步一步說明: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> // 添加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> // 添加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
ios webview裡的按鈕事件怎實現返回app應用
IOS學習之WebView的使用
1、使用UIWebView載入網頁
運行XCode 4.3,建立一個Single View Application,命名為WebViewDemo。
2、載入WebView
在ViewController.h添加WebView成員變數和在ViewController.m添加實現
手機的網路環境是即時變化的,網路慢的時候,怎麼提示使用者網頁正在開啟呢?在網頁開啟出錯的時候怎麼提示使用者呢?這時候我們就需要知道網頁什麼時候開啟的,
什麼時候載入完成,什麼時候出錯了。那麼我們需要實現這個<UIWebViewDelegate>協議
3、實現協議,在ViewController.h修改如下:
按住control+command+向上鍵,切換到ViewController.m檔案,這是我們在檔案中打入- (void) webView,就能看到如下實現方法:
UIWebView中幾個重要的函數
1.- (void )webViewDidStartLoad:(UIWebView *)webView 網頁開始載入的時候調用
2.- (void )webViewDidFinishLoad:(UIWebView *)webView 網頁載入完成的時候調用
3.- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error 網頁載入錯誤的時候調用
4、實現這三個方法,加入NSLog。
先在viewDidLoad 的webView執行個體化下面加上
[webView setDelegate:self];設定代理。這樣上面的三個方法才能得到回調。
三個方法實現如下:
運行列印:
2012-06-23 15:20:29.728 WebViewDemo[1001:f803] webViewDidStartLoad
2012-06-23 15:20:29.991 WebViewDemo[1001:f803] webViewDidFinishLoad
那我們試試error情況,把wifi關掉,運行列印結果:
2012-06-23 15:23:58.939 WebViewDemo[1087:f803] webViewDidStartLoad
2012-06-23 15:23:59.016 WebViewDemo[1087:f803] webViewDidFinishLoad
請求結果不變,為什麼關掉網路還成功了呢?緩衝?我換163.com試試,這是真正的結果出來了:
2012-06-23 15:24:41.131 WebViewDemo[1134:f803] webViewDidStartLoad
2012-06-23 15:24:41.149 WebViewDemo[1134:f803] didFailLoadWithError:Error Domain=NSURLErrorDomain Code=-1009 "The Internet connection appears to be offline." UserInfo=0x6b41660 {NSErrorFailingURLStringKey=www.163.com/, NSErrorFailingU......餘下全文>>
ios uiwebview可以載入json格式的資料?
可以的,但是你要將json轉換成你的資料結構,xml也一樣,不可能直接去load一個json或者xml資料