IOS-listen to keyboard input, move up or restore the view as a whole-avoid input obstruction

Source: Internet
Author: User

IOS-listen to keyboard input, move up or restore the view as a whole-avoid input obstruction

1. initialize the input box. Listen to the input box to start editing and end editing.

// Password input box UITextField * loginputpasswordstr = [UITextField inputTextWithImage: [UIImage imageNamed: @ "Password"] placeholderText: NSLocalizedString (@ "Password", nil)]; forbidden. frame = RectMakeWithPercent (24/375. 0, 385/667. 0, 327/375. 0, 48/667. 0); // set the keyboard type loginInputPasswordStr. keyboardType = UIKeyboardTypeDefault; // set the dark text [loginputpasswordstr keys: YES]; // The text in the password input box starts to enter [loginInputPasswordStr addTarget: self action: @ selector (timer) forControlEvents: users]; // listen to the password input box text end input [loginputpasswordstr addTarget: self action: @ selector (loginputpasswordstrinputend) forControlEvents: UIControlEventEditingDidEnd]; [self. view addSubview: loginputpasswordstr];

2. When editing, move the image to the image. When editing is finished or the screen is clicked, restore the initial position.

// LoginInputPasswordStr listening method-(void) loginInputPasswordStrInputBegin {[self moveView];}-(void) loginInputPasswordStrInputEnd {[self resumeView];} // click the screen to cancel the input, restore view initial position-(void) touchesBegan :( NSSet *) touches withEvent :( UIEvent *) event {[self. view endEditing: YES]; [self resumeView];}

3. Subject method!

# Pragma mark-click the input box to move up/restore the entire view. // restore the original view position-(void) resumeView {NSLog (@ "restore the original view position before self. view. origin. y: % f ", self. view. origin. y); // when the view is moved up by 130 pixels-> if (self. view. frame. origin. y =-130) {NSTimeInterval animationDuration = 0.40f; [UIView beginAnimations: @ "ResizeForKeyboard" context: nil]; [UIView setAnimationDuration: animationDuration]; // if the current View is a parent View, Y is the height of 20 pixels. If the current View is a child View of another View, the float fX = self of Y is dynamically adjusted. view. frame. origin. x; float fY = self. view. frame. origin. y+ 130; float width = self. view. frame. size. width; float height = self. view. frame. size. height; // move down 130 units and Set self according to the actual situation. view. frame = CGRectMake (fX, fY, width, height); [UIView commitAnimations] ;}// move the window-(void) moveView {NSLog (@ "self before moving the window. view. origin. y: % f ", self. view. origin. y); // only when the view is in the initial position-> if (self. view. origin. y = 0) {NSTimeInterval animationDuration = 0.40f; [UIView beginAnimations: @ "ResizeForKeyboard" context: nil]; [UIView setAnimationDuration: animationDuration]; float fX = self. view. frame. origin. x; float fY = self. view. frame. origin. y-130; float width = self. view. frame. size. width; float height = self. view. frame. size. height; // move up 130 units self. view. frame = CGRectMake (fX, fY, width, height); [UIView commitAnimations] ;}}

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.