Uitextfield solution Prevents keyboard blocking during iOS development

Source: Internet
Author: User

Recently transferred to iOS development, I found that If IOS uitextfield is at the bottom of the screen, the keyboard cannot automatically adjust the interface layout, You need to manually adjust the location, therefore, I want to write a general method to automatically adapt the keyboard position on each interface. In this way, I do not need to operate on the Interface location on each interface, the specific solution is as follows:

1. First create a uiviewcontroller. This uiviewcontroller serves as the parent class, so that each interface in the future will inherit this interface and implement a delegate in this interface. The Code is as follows:

@interface BaseViewController : UIViewController <UITextFieldDelegate>

2. in this baseviewcontroller. in the M file, the two methods in the real uitextfielddelegate are textfielddidbeginediting (start to edit uitextfield and textfielddidendediting (end to edit uitextfield). We all know that the iPhone keyboard is fixed and comes with the System, there is no third-party input method, so the keyboard height is fixed at 216. When we start editing, calculate whether the height of the current uitextfield is 216 relative to the bottom of the screen (that is, whether the bottom edge of uitextfield has 216 length relative to the bottom of the screen). If it is less than 216, you just need to move the overall view up to 216. When we finish editing, we can reverse the previously added height. The Code is as follows:

// Set the animation effect of the adjusted interface // set the animation effect of the adjusted Interface

Int prewtag; // edit the tag of the previous uitextfield, which must be defined in the XIB file or added to the program. The tag of the two controls cannot be the same as float prewmovey; // The height to be moved during editing // The following two methods are used to prevent textfiled from blocking the keyboard/** start to edit the uitextfield Method */-(void) textfielddidbeginediting :( uitextfield *) textfield {cgrect textframe = textfield. frame; float texty = textframe. origin. Y + textframe. size. height; float bottomy = self. view. frame. size. height-texty; If (bottomy> = 216) // you can check whether the current height is 216. If it exceeds, you do not need to move the view height of the main interface {prewtag =-1; return;} prewtag = textfield. tag; float Movey = 216-bottomy; prewmovey = Movey; nstimeinterval animationduration = 0.30f; cgrect frame = self. view. frame; frame. origin. y-= Movey; // move the frame up on the Y axis of the view. size. height + = Movey; // The height of the view increases by self. view. frame = frame; [uiview beginanimations: @ "resizeview" context: Nil]; [uiview setanimationduration: animationduration]; self. view. frame = frame; [uiview commitanimations]; // sets the animation effect of the adjusted interface}/** ends the uitextfield editing method to restore the height of the original interface */-(void) textfielddidendediting :( uitextfield *) textfield {If (prewtag =-1) // when the edited view is not the view to be moved {return;} float Movey; nstimeinterval animationduration = 0.30f; cgrect frame = self. view. frame; If (prewtag = textfield. tag) // when the tag of the edited view is moved last time {// restore interface Movey = prewmovey; frame. origin. Y + = Movey; frame. size. height-= Movey; self. view. frame = frame;} // self. view to the original location [uiview beginanimations: @ "resizeview" context: Nil]; [uiview setanimationduration: animationduration]; self. view. frame = frame; [uiview commitanimations]; [textfield resignfirstresponder];}

3. In the above Code, we have added a delegate for uitextfield editing listening. Next we will let our subclass uiviewcontroller listen for the delegate.

Code:

IDNameField.delegate = self;

Idnamefield is a uitextfield in uiviewcontroller, a subclass that inherits baseviewcontroller. Once the above operations are implemented, our uitextfield can be automatically adapted and adjusted on each interface, this prevents the keyboard from blocking uitextfield.
Sky. You are welcome to repost it. Please respect the author's experience! If you have any questions, contact:

QQ: 840950105

Email: vipa1888@163.com



Related Article

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.