iOS keyboard monitor and get keyboard height

Source: Internet
Author: User

When the text is entered, the interface pops up the keyboard. Sometimes, when the text input box is too low to be obscured by the keyboard, the user cannot see the text content of the input box, which makes the user experience too low.

So we need to monitor the keyboard and get the keyboard height, adjust the interface or text box height to handle. For example, if the text input box is too low:

     

In cases like this, the input box is obscured by the keyboard if it is not processed. At this point, we need to listen for keyboard events, get the keyboard height, and adjust the text frame view Height:

1 #import "ViewController.h"2 3 @interfaceViewcontroller () <UITextFieldDelegate>4@property (Weak, nonatomic) Iboutlet Uitextfield *Inputfield;5 //Drag line Gets the view constraint object6@property (Weak, nonatomic) Iboutlet Nslayoutconstraint *Tobottom;7 8 @end9 Ten @implementationViewcontroller One  A- (void) Viewdidload { - [Super Viewdidload]; -      theSelf.inputfield.Delegate=Self ; -   -     //Add notification Supervisor hear keyboard eject/exit -[[Nsnotificationcenter Defaultcenter] addobserver:self selector: @selector (keyboardaction:) Name: UikeyboardwillshownotificationObject: nil]; +[[Nsnotificationcenter Defaultcenter] addobserver:self selector: @selector (keyboardaction:) Name: UikeyboardwillhidenotificationObject: nil]; - } +  A //Click the non-TextField area to cancel the first responder at- (void) Touchesbegan: (Nsset<uitouch *> *) touches withevent: (Uievent *)Event{ - [Self.inputfield Resignfirstresponder]; - } -  - //Click the Keyboard return button to cancel the first responder --(BOOL) Textfieldshouldreturn: (Uitextfield *) textfield{ in [Self.inputfield Resignfirstresponder]; -     returnYES; to } +  - //Click the button to take the first responder the-(Ibaction) Okaction: (UIButton *) Sender { * [Self.inputfield Resignfirstresponder]; $ }Panax Notoginseng  - //Keyboard Listener Events the- (void) Keyboardaction: (nsnotification*) sender{ +     //get the keyboard frame by notifying the object: [Value Cgrectvalue] ANsdictionary *useinfo =[Sender UserInfo]; theNsvalue *value =[Useinfo Objectforkey:uikeyboardframeenduserinfokey]; +      -     //< Note > Constrained controls change the frame's handling by changing the constraint value $     if([Sender.name isequaltostring:uikeyboardwillshownotification]) { $Self.toBottom.constant =[value cgrectvalue].size.height; -}Else{ -Self.toBottom.constant =0; the     } - }Wuyi  the- (void) didreceivememorywarning { - [Super didreceivememorywarning]; Wu } -  About @end

After processing like this, the position of the input box view can be adjusted with the popup/exit of the keyboard:

  

iOS keyboard monitor and get keyboard height

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.