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] ;}}