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