IOS UIWebView Keyboard Handling

Source: Internet
Author: User

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

If you have the following questions, this article may help you.

    1. The keyboard covers the UIWebView.
    2. How to remove the keyboard by dragging UIWebView.
    3. When the keyboard appears, the content contents of the UIWebView move up, and the focused text box is outside the viewable area of the UIWebView.
    4. How to prevent the content of UIWebView from moving up when the keyboard pops up.
    5. Coordinates cannot be obtained in UIWebView to calculate contentoffset to get the results you want to show.

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


Step by Step instructions:1. Remove the keyboard

Just click the HTML TextBox control inside the UIWebView and the keyboard will pop up automatically. Of course you need to get the information of the keyboard (height, etc.), method or use Uiviewcontroller+notification way, the code is as follows:

Uikeyboardwillshownotification and uikeyboardwillhidenotification The name of the iOS system post notification when the keyboard is ejected or removed. You only need to define self as the recipient of this notification. Viewwillappear: And Viewwilldisappear: It should be clear to everyone that the two methods are executed after self loadview and Removefromsuperview respectively.    Special Note: Here the object parameter needs to be nil, otherwise cannot take the keyboard 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];    Here you get the keyboard frame//Your operation, such as keyboard appears, control view up shift etc}-(void) Keyboardwillhide: (nsnotification *) Notification {//Get info with above method Your actions, such as keyboard removal, control view restore, etc.}

2. Remove the keyboard by dragging UIWebView

Seeing a lot of people on the internet has done a lot to implement this feature, but in iOS7 Apple has provided these to us, the code is as follows:

Self.webView.scrollView.keyboardDismissMode = Uiscrollviewkeyboarddismissmodeondrag; Remove keyboard when dragging

If it is iOS7 below, please refer to 6 to set, approximate idea, first add a private flag to indicate whether the keyboard is present, when present, through 6 to get the event off the keyboard.

3. The keyboard covers the UIWebViewThis solution can be1In the Keyboardwillshow: inside operation, by changing the origin of WebView to achieve.4. When the keyboard appears, the content contents of the UIWebView move up, and the focused text box is beyond the viewable area of the UIWebView.

In the UIWebView, as long as the keyboard appears, UIWebView will definitely move upward, as for the fit is not good to say, if not appropriate, only with disable automatic movement.

5. How to prevent the content of UIWebView from moving up when the keyboard pops up

This method, I also looked for a long time, but still found, thanks to powerful netizens, the code is as follows:

@interface xxx:uiviewcontroller<uiscrollviewdelegate>//Add uiscrollviewdelegate, step 1self.webview.scrollview.delegate = self; Registration Agent, Step 2-(uiview*) Viewforzoominginscrollview: (uiscrollview*) scrollview{//Implement proxy method, step 3        return nil;}

6. How to get the click coordinates in UIWebView

As we all know, UIWebView will eat all the touch events, or there will not be so many people to make JavaScript, but can not be set does not mean that can not be replaced by another way, the idea: to WebView superview add gestures, Then by implementing multi-gesture filtering settings, why do you want to set up multi-gesture filtering? I would like to explain here, because UIWebView default has its own gesture, it will intercept your gestures, so that superview can not receive gestures, the code is as follows:

 @interface xxx:uiviewcontroller<uigesturerecognizerdelegate>//Add uigesturerecognizerdelegate, step 1//add gesture, 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];//set filter, Ruturn Yes to receive at the same time, this gesture can be through the webview, so that your superview can also receive, step (BOOL) Gesturerecognizer: (Uigesturerecognizer *) Gesturerecognizer Shouldrecognizesimultaneouslywithgesturerecognizer: ( Uigesturerecognizer *) othergesturerecognizer{return YES; -(void) Webtap: (UITapGestureRecognizer *) sender{cgpoint tappoint = [Sender LocationInView:self.webView.scrollView] ; Gets the coordinates relative to the webview, and if changed to Self.view, gets the coordinates relative to Superview, step 4 NSLog (@ "Tappoint x:%f y:%f", Tappoint.x,tappoint.y);} 

UIWebView keyboard processing can think of only these, welcome to add.

BB: Reprint please indicate the sourcehttp://blog.csdn.net/assholeu/article/details/38714123
Data reference:Thankhttp://blog.csdn.net/abel_tu/article/details/12134261


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.