IOS handles keyboard occlusion textfield, TextView issues

Source: Internet
Author: User
<span id="Label3"></p><p><p>Before dealing with the problem of keyboard occlusion is handled separately in each controller, this is really very troublesome, today in the project to think of their own encapsulation of a, record this "ups and downs" process.</p></p><p><p>The idea is to calculate the text edit control frame with the keyboard frame, and if masking moves the controller VIEW.</p></p><p><p>Create controller Class: Wkavoidkeyboardviewcontroller</p></p><p><p>  </p></p><pre><span style="color: #008080;"><span style="color: #008080;">1</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">#import</span></span><UIKit/UIKit.h><span style="color: #008080;"><span style="color: #008080;">2</span></span> <span style="color: #008080;"><span style="color: #008080;">3</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">@interface</span></span><span style="color: #000000;"><span style="color: #000000;">Wkavoidkeyboardviewcontroller:uiviewcontroller</span></span><span style="color: #008080;"><span style="color: #008080;">4</span></span> <span style="color: #008080;"><span style="color: #008080;">5</span></span>@property (nonatomic, Strong) Uitextfield *<span style="color: #000000;"><span style="color: #000000;">edittextfield;</span></span><span style="color: #008080;"><span style="color: #008080;">6</span></span>@property (nonatomic, Strong) Uitextview *<span style="color: #000000;"><span style="color: #000000;">edittextview;</span></span><span style="color: #008080;"><span style="color: #008080;">7</span></span> <span style="color: #008080;"><span style="color: #008080;">8</span></span>- (<span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span>) hidekeyboard: (nsnotification *<span style="color: #000000;"><span style="color: #000000;">) noti;</span></span><span style="color: #008080;"><span style="color: #008080;">9</span></span>- (<span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span>) showkeyboard: (nsnotification *<span style="color: #000000;"><span style="color: #000000;">) noti;</span></span><span style="color: #008080;"><span style="color: #008080;">Ten</span></span> <span style="color: #008080;"><span style="color: #008080;"></span> one</span> <span style="color: #008080;"><span style="color: #008080;"></span> a</span> <span style="color: #0000ff;"><span style="color: #0000ff;">@end</span></span><span style="color: #008080;"><span style="color: #008080;"></span> -</span> <span style="color: #008080;"><span style="color: #008080;"></span> -</span> <span style="color: #008080;"><span style="color: #008080;"></span> the</span> <span style="color: #0000ff;"><span style="color: #0000ff;">#import</span></span> <span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #800000;"><span style="color: #800000;">WKAvoidKeyboardViewController.h</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #008080;"><span style="color: #008080;"></span> -</span> <span style="color: #008080;"><span style="color: #008080;"></span> -</span> <span style="color: #0000ff;"><span style="color: #0000ff;">#define</span></span>Getosversion [[uidevice currentdevice].systemversion floatvalue]<span style="color: #008080;"><span style="color: #008080;"></span> -</span> <span style="color: #008080;"><span style="color: #008080;"></span> +</span> <span style="color: #0000ff;"><span style="color: #0000ff;">#define</span></span>Gettransformdistance (Distance) (getosversion < 7.1?) Distance/2: Distance)<span style="color: #008080;"><span style="color: #008080;"></span> -</span> <span style="color: #008080;"><span style="color: #008080;"></span> +</span> <span style="color: #0000ff;"><span style="color: #0000ff;">@interface</span></span>Wkavoidkeyboardviewcontroller () <uitextfielddelegate, uitextviewdelegate><span style="color: #008080;"><span style="color: #008080;"></span> a</span> <span style="color: #008080;"><span style="color: #008080;"></span> at</span> <span style="color: #0000ff;"><span style="color: #0000ff;">@end</span></span><span style="color: #008080;"><span style="color: #008080;"></span> -</span> <span style="color: #008080;"><span style="color: #008080;"></span> -</span> <span style="color: #0000ff;"><span style="color: #0000ff;">@implementation</span></span><span style="color: #000000;"><span style="color: #000000;">Wkavoidkeyboardviewcontroller</span></span><span style="color: #008080;"><span style="color: #008080;"></span> -</span>- (<span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span>) touchesbegan: (nsset *) touches withevent: (uievent *)<span style="color: #0000ff;"><span style="color: #0000ff;">Event</span></span><span style="color: #008080;"><span style="color: #008080;"></span> -</span> <span style="color: #000000;"><span style="color: #000000;">{</span></span><span style="color: #008080;"><span style="color: #008080;"></span> -</span> <span style="color: #000000;"><span style="color: #000000;">[self.view endediting:yes];</span></span><span style="color: #008080;"><span style="color: #008080;"></span> in</span> <span style="color: #000000;"><span style="color: #000000;">}</span></span><span style="color: #008080;"><span style="color: #008080;"></span> -</span> <span style="color: #0000ff;"><span style="color: #0000ff;">@end</span></span></pre><p><p></p></p><p><p>Step 1: Get the currently edited text control by notification</p></p><pre><pre><span style="color: #008000;">//</span> <span style="color: #008000;">Registration Notice</span> <span style="color: #0000ff;">Object</span> <span style="color: #000000;">: nil]; </span> <span style="color: #0000ff;">Object</span> <span style="color: #000000;">: nil]; </span> <span style="color: #0000ff;">Object</span>: nil];</pre></pre><pre><pre><span> [[nsnotificationcenter defaultcenter] addobserver:self selector: @selector (textfieldbeginediting:) name: Uitextviewtextdidbegineditingnotification object:nil];</span></pre></pre><pre><pre>-(<span style="color: #0000ff;">void</span>) textfieldbeginediting: (nsnotification *<span style="color: #000000;">) noti{ </span>= Noti. <span style="color: #0000ff;"> Object</span><span style="color: #000000;">; </span> =<span style="color: #000000;"> nil;}</span> -(<span style="color: #0000ff;">void</span>) textviewbeginediting: (nsnotification *<span style="color: #000000;">) noti{ </span>= Noti. <span style="color: #0000ff;"> Object</span><span style="color: #000000;">; </span> =<span style="color: #000000;"> nil;}</span></pre></pre><p><p></p></p><p><p>Step 2: Notify get keyboard height</p></p><p><p>Step 3: Calculate whether you need to move</p></p><p><p>  </p></p><pre><span style="color: #0000ff;"><span style="color: #0000ff;">#pragma</span></span>Mark-keyboard dodge-(<span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span>) showkeyboard: (nsnotification *<span style="color: #000000;"><span style="color: #000000;">) Noti{self.view.transform</span></span>=<span style="color: #000000;"><span style="color: #000000;">cgaffinetransformidentity; UIView</span></span>*editview = _edittextview?<span style="color: #000000;"><span style="color: #000000;">_edittextview: _edittextfield; CGRect Tfrect</span></span>=<span style="color: #000000;"><span style="color: #000000;">[editview.superview ConvertRect:editView.frame toView:self.view]; Nsvalue</span></span>*value = noti.userinfo[<span style="color: #800000;"><span style="color: #800000;">@"</span></span><span style="color: #800000;"><span style="color: #800000;">Uikeyboardframeenduserinfokey</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #000000;"><span style="color: #000000;">]; NSLog (</span></span><span style="color: #800000;"><span style="color: #800000;">@"</span></span><span style="color: #800000;"><span style="color: #800000;">%@</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #000000;"><span style="color: #000000;">, value); CGRect KEYBOARDF</span></span>=<span style="color: #000000;"><span style="color: #000000;">[value cgrectvalue]; CGFloat Animationtime</span></span>= [noti.userinfo[<span style="color: #800000;"><span style="color: #800000;">@"</span></span><span style="color: #800000;"><span style="color: #800000;">Uikeyboardanimationdurationuserinfokey</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #000000;"><span style="color: #000000;">] floatvalue]; CGFloat _editmaxy</span></span>=<span style="color: #000000;"><span style="color: #000000;">Cgrectgetmaxy (tfrect); CGFloat _keyboardminy</span></span>=<span style="color: #000000;"><span style="color: #000000;">Cgrectgetminy (keyboardf); NSLog (</span></span><span style="color: #800000;"><span style="color: #800000;">@"</span></span><span style="color: #800000;"><span style="color: #800000;">%f%f</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #000000;"><span style="color: #000000;">, _editmaxy, _keyboardminy); </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">if</span></span>(_keyboardminy <<span style="color: #000000;"><span style="color: #000000;">_editmaxy) {cgfloat movedistance</span></span>= _editmaxy-<span style="color: #000000;"><span style="color: #000000;">_keyboardminy; [UIView animatewithduration:animationtime animations:</span></span>^<span style="color: #000000;"><span style="color: #000000;">{self.view.transform</span></span>= Cgaffinetransformtranslate (self.view.transform,<span style="color: #800080;"><span style="color: #800080;">0</span></span>, -<span style="color: #000000;"><span style="color: #000000;">movedistance); }]; }}</span></span>- (<span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span>) hidekeyboard: (nsnotification *<span style="color: #000000;"><span style="color: #000000;">) noti{</span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">NSLog (@ "%@", noti);</span></span>Self.view.transform =<span style="color: #000000;"><span style="color: #000000;">cgaffinetransformidentity;}</span></span></pre><p><p>Preliminary test: uitextfiled success, and then to the uitextview, the problem of the pit daddy thick line =.=, uitextviewtextdidbegineditingnotification send time is after the keyboard pop-up notice, Cause the first click TextView No use, Click the second time to produce Effects. so, I started to try to use Textview's delegate to do, take for granted the proxy method</p></p><p><p>-(void) textviewdidbeginediting: (uitextview *) TextView</p></p><pre><pre><span style="color: #008080;">1</span> -(<span style="color: #0000ff;">void</span>) textviewdidbeginediting: (uitextview *<span style="color: #000000;">) TextView</span><span style="color: #008080;">2</span><span style="color: #000000;">{</span><span style="color: #008080;">3</span> <span style="color: #008080;">4</span> }</pre></pre><p><p>Disappointing is textviewdidbeginediting: the method call is still in the keyboard notification pop-up, then call, at this time the mind is: beep dog, let people how to play! Let's look at other ways. So in the proxy method, I see</p></p><p><p>-(BOOL) textviewshouldbeginediting: (uitextview *) TextView</p></p><p><p>After the attempt, this method is actually called before the keyboard POPs up, and the next step is to set up an agent Problem.</p></p><p><p>Set the Proxy method as Follows:</p></p><pre><span style="color: #008080;"><span style="color: #008080;">1</span></span>- (<span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span>) Searchtextviewwithview: (UIView *<span style="color: #000000;"><span style="color: #000000;">) View</span></span><span style="color: #008080;"><span style="color: #008080;">2</span></span> <span style="color: #000000;"><span style="color: #000000;">{</span></span><span style="color: #008080;"><span style="color: #008080;">3</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;"></span> for</span>(UIView *subview<span style="color: #0000ff;"><span style="color: #0000ff;">inch</span></span><span style="color: #000000;"><span style="color: #000000;">View.subviews)</span></span><span style="color: #008080;"><span style="color: #008080;">4</span></span> <span style="color: #000000;"><span style="color: #000000;"> {</span></span><span style="color: #008080;"><span style="color: #008080;">5</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">if</span></span>([subview Iskindofclass:[uitextview<span style="color: #0000ff;"><span style="color: #0000ff;">class</span></span><span style="color: #000000;"><span style="color: #000000;">]]) {</span></span><span style="color: #008080;"><span style="color: #008080;">6</span></span>((uitextview *) subview).<span style="color: #0000ff;"><span style="color: #0000ff;">Delegate</span></span>=<span style="color: #000000;">self <span style="color: #000000;">;</span></span><span style="color: #008080;"><span style="color: #008080;">7</span></span> <span style="color: #000000;"><span style="color: #000000;"> }</span></span><span style="color: #008080;"><span style="color: #008080;">8</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">if</span></span>([subview Iskindofclass:[uitextfield<span style="color: #0000ff;"><span style="color: #0000ff;">class</span></span><span style="color: #000000;"><span style="color: #000000;">]]) {</span></span><span style="color: #008080;"><span style="color: #008080;">9</span></span>((uitextfield *) subview).<span style="color: #0000ff;"><span style="color: #0000ff;">Delegate</span></span>=<span style="color: #000000;">self <span style="color: #000000;">;</span></span><span style="color: #008080;"><span style="color: #008080;">Ten</span></span> <span style="color: #000000;"><span style="color: #000000;"> }</span></span><span style="color: #008080;"><span style="color: #008080;"></span> one</span> <span style="color: #000000;"><span style="color: #000000;">[self searchtextviewwithview:subview];</span></span><span style="color: #008080;"><span style="color: #008080;"></span> a</span> <span style="color: #000000;"><span style="color: #000000;"> }</span></span><span style="color: #008080;"><span style="color: #008080;"></span> -</span>}</pre><p><p>At this point, use: inherit wkavoidkeyboardviewcontroller, if it is a text control created with the story version, do nothing, if it is created with code, You need to call the Searchtextviewwithview method in Viewdidload</p></p><p><p>Complete Code: https://github.com/WuKongCoo1/AvoidKeyboardDemo.git</p></p><p><p></p></p><p><p>IOS handles keyboard occlusion textfield, TextView issues</p></p></span>

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.