The keyboard will overwrite the text box when it pops up. How to solve _ios

Source: Internet
Author: User

In some Web landing interface, we often see, the keyboard appears and hidden operations, then based on how the code is implemented? The following small prepared a specific code introduction, hereby share to the cloud Habitat Community Platform for your reference

First to show you the effect of the picture:

The specific code looks like this:

#import "ViewController.h" #import "uiview+frameextension.h"//can write on its own, later with convenient #define Kdeviceheight [UIScreen mainscreen ].bounds.size.height @interface Viewcontroller () @end @implementation Viewcontroller-(void) viewdidload {[Super Viewdi
Dload];
Set the background color of the view Self.view.backgroundColor = [Uicolor Lightgraycolor];
Adding the first text box assumes position uitextfield *firstfield = [[Uitextfield alloc]initwithframe:cgrectmake (50, 300, 200, 40)];
Firstfield.backgroundcolor = [Uicolor Whitecolor];
[Self.view Addsubview:firstfield]; Add first text box Uitextfield *secondfield = [[Uitextfield alloc]initwithframe:cgrectmake (firstfield.x, FirstField.bottom +
Firstfield.width, Firstfield.height)];
[Self.view Addsubview:secondfield];
Secondfield.backgroundcolor = [Uicolor Whitecolor]; Registers the keyboard display notification [[Nsnotificationcenter Defaultcenter] addobserver:self selector: @selector (showkeyboard:) Name:
Uikeyboardwillshownotification Object:nil]; Register a keyboard hidden alert [[Nsnotificationcenter Defaultcenter] addobserver:self selector: @selector (HIDEkeyboard:) name:uikeyboardwillhidenotification Object:nil]; When the keyboard pops up, execute this method,-(void) Showkeyboard: (nsnotification *) notification{//Define a text box that points to the text box being edited, which is the text box that pops up the keyboard Uitextfield *
Txtfield; This time traversing all the views of the current view, the Subviews array holds all the child views for the current view for (UIView *subview in self.view.subviews) {//If the child view is a text box, The Iskindofclass method can determine whether a variable is a variable of a certain type if ([Subview Iskindofclass:[uitextfield class]]) {//First convert the child view to a text box Uitextfield *
Tempfield = (Uitextfield *) Subview;
To determine if this text box is editing if (tempfield.isediting) {//If this text box is being edited, that is the text box I am looking for, interrupt loop Txtfield = Tempfield; break;}
} NSLog (@ "%@", notification);
Gets the UserInfo property of the notification nsdictionary *userinfodict = Notification.userinfo;
Get the keyboard bounds nsvalue *value = [Userinfodict Objectforkey:uikeyboardboundsuserinfokey] by the UserInfo property of the keyboard notification;
The size of the keyboard cgsize keyboardsize = [value cgrectvalue].size;
Keyboard height CGFloat keyboardheight = keyboardsize.height;
CGFloat offset = Kdeviceheight-keyboardheight-txtfield.bottom; if (Offset < 0) {//You need to move offset = offset-10 in this case; Saves the height of the move up [UIView animatewithduration:0.5 animations:^{self.view.transform = cgaffinetransformmaketranslation (0,
Offset);
}];
}}-(void) Hidekeyboard: (nsnotification *) notification{[UIView animatewithduration:2 animations:^{
Self.view.transform = cgaffinetransformidentity;
}]; ///Click on the screen blank to hide the keyboard-(void) Touchesbegan: (Nsset<uitouch *> *) touches withevent: (uievent *) event{[Self.view
Endediting:yes]; } @end

About keyboard pop-up and hidden code for everyone to introduce here, I hope to help you!

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.