The keyboard appears in the hidden (solve the problem that the text box will be overwritten when the keyboard pops up, code implementation), the keyboard text box

Source: Internet
Author: User

The keyboard appears in the hidden (solve the problem that the text box will be overwritten when the keyboard pops up, code implementation), the keyboard text box

Hide the keyboard (solve the problem that the text box will be overwritten when the keyboard pops up, and implement the Code)

 

# Import "ViewController. h"

# Import "UIView + FrameExtension. h" // you can write it by yourself for later convenience.

# Define kDeviceHeight [UIScreen mainScreen]. bounds. size. height

@ Interface ViewController ()

@ End

@ Implementation ViewController

-(Void) viewDidLoad {

[Super viewDidLoad];

// Set the view background color

Self. view. backgroundColor = [UIColor lightGrayColor];

// Add the assumption location of the first text box

UITextField * firstField = [[UITextField alloc] initWithFrame: CGRectMake (50,300,200, 40)];

FirstField. backgroundColor = [UIColor whiteColor];

[Self. view addSubview: firstField];

// Add the first text box

UITextField * secondField = [[UITextField alloc] initWithFrame: CGRectMake (firstField. x, firstField. bottom + 50, firstField. width, firstField. height)];

[Self. view addSubview: secondField];

SecondField. backgroundColor = [UIColor whiteColor];

// Register the notification displayed on the keyboard

[[Nsicationcenter center defacenter center] addObserver: self selector: @ selector (showKeyboard :) name: UIKeyboardWillShowNotification object: nil];

// Register the hidden notifications on the keyboard

[[Nsicationcenter center defacenter center] addObserver: self selector: @ selector (hideKeyboard:) name: UIKeyboardWillHideNotification object: nil];

}

 

// Execute this method when the keyboard pops up,

-(Void) showKeyboard :( NSNotification *) notification {

// Define a text box pointing to the text box being edited, that is, the text box of the pop-up keyboard

UITextField * txtField;

// This time, all the subViews in the current view are traversed. The subViews array stores all the subViews in the current view.

For (UIView * subView in self. view. subviews ){

// If this sub-view is a text box, the isKindOfClass method can determine whether a variable is of a certain type.

If ([subView isKindOfClass: [UITextField class]) {

// Convert the subview into a text box first

UITextField * tempField = (UITextField *) subView;

// Determine whether the text box is being edited.

If (tempField. isEditing ){

// If the text box is being edited, it is the text box that I am looking.

TxtField = tempField;

Break;

}

}

}

NSLog (@ "% @", notification );

// Get the userInfo attribute of the notification

NSDictionary * userInfoDict = notification. userInfo;

// Obtain the keyboard bounds through the userInfo attribute of the keyboard notification

NSValue * value = [userInfoDict objectForKey: UIKeyboardBoundsUserInfoKey];

// Keyboard size

CGSize keyboardSize = [value CGRectValue]. size;

// Keyboard height

CGFloat keyboardHeight = keyboardSize. height;

CGFloat offset = kDeviceHeight-keyboardHeight-txtField. bottom;

If (offset <0) {// in this case, you need to move up

Offset = offset-10; // Save 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;

}];

}

// Hide the keyboard when the screen is blank

-(Void) touchesBegan :( NSSet <UITouch *> *) touches withEvent :( UIEvent *) event {

[Self. view endEditing: YES];

}

@ End

 

The source file can be downloaded here, hope to help you: http://pan.baidu.com/s/1pLms9zP

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.