Zookeeper
I encountered a conventional problem during ios development. UITextField was masked by the keyboard. I searched some materials on the Internet and found two solutions written by my predecessors. solution 1: workshop? Mod = space & uid = 107838 & do = blog & id = 44715. The second solution is to directly move the entire view to the keyboard width. In some scenarios, it is not very appropriate. I have made some changes based on the two parameters. The specific steps are as follows:
I have defined a basic ViewController-BaseViewController.
The. h file content is as follows:
# Import
@ Interface BaseViewController: UIViewController {
UITextField * _ checkText; // used to identify which UITextField is clicked
@ Property (nonatomic) UITextField * checkText;
-(Void) moveInputBarWithKeyboardHeight :( float) _ CGRectHeightwithDuration :( NSTimeInterval) _ NSTimeInterval;
@ End
The. m file is as follows:
# Import "BaseViewController. h"
@ Implementation BaseViewController
-(Void) viewDidLoad {
[Super viewDidLoad];
// Registration notification
[[Nsicationcenter center defacenter center] addObserver: selfselector: @ selector (keyboardWillShow :) name: UIKeyboardWillShowNotificationobject: nil];
[[Nsicationcenter center defacenter center] addObserver: selfselector: @ selector (keyboardWillHide :) name: UIKeyboardWillHideNotificationobject: nil];
// Keyboard height change notification, added by ios5.0
# Ifdef _ IPHONE_5_0
Float version = [[[UIDevice currentDevice] systemVersion] floatValue];
If (version> = 5.0 ){
[[Nsicationcenter center defacenter center] addObserver: self selector: @ selector (keyboardWillShow :) name: UIKeyboardWillChangeFrameNotification object: nil];
}
# Endif
}
# Pragma mark began to edit UITextField. I tried this method and was called before keyboardWillShow.
-(Void) textFieldDidBeginEditing :( UITextField *) textField {
_ CheckText = textField; // sets the clicked object.
}
# Pragma mark-method called when the keyboard pops up
# Pragma mark Responding to keyboard events
-(Void) keyboardWillShow :( NSNotification *) notification {
If (nil = _ checkText ){
Return;
}
/*
Reduce the size of the text view so that it's not obscured by thekeyboard.
Animate the resize so that it's in sync with the appearance of thekeyboard.
*/
NSDictionary * userInfo = [notification userInfo];
// Get the origin of the keyboard when it's displayed.
NSValue * aValue = [userInfo objectForKey: UIKeyboardFrameEndUserInfoKey];
// Get the top of the keyboard as the y coordinate of its origin inself's view's coordinate system. the bottom of the text view's frame shouldalign with the top of the keyboard's final position.
CGRect keyboardRect = [aValue CGRectValue];
// Get the duration of the animation.
NSValue * animationDurationValue = [userInfoobjectForKey: UIKeyboardAnimationDurationUserInfoKey];
NSTimeInterval animationDuration;
[AnimationDurationValue getValue: & animationDuration];
CGRect textFrame = _ checkText. frame; // The current location of UITextField
Float textY = textFrame. origin. y + textFrame. size. height; // obtain the height of the Bottom Border of UITextField from the top
Float bottomY = self. view. frame. size. height-textY; // obtain the distance from the bottom border to the bottom.
If (bottomY> = keyboardRect. size. height) {// default keyboard height. if it is greater than this height, the system returns
Return;
}
Float moveY = keyboardRect. size. height-bottomY;
// Animate the resize of the text view's frame in sync with the keyboard 'sappearance.
[Self moveInputBarWithKeyboardHeight: moveYwithDuration: animationDuration];
}
// Method called when the keyboard is hidden
(Void) keyboardWillHide :( NSNotification *) notification {
NSDictionary * userInfo = [notification userInfo];
/*
Restore the size of the text view (fill self's view ).
Animate the resize so that it's in sync with the disappearance of thekeyboard.
*/
NSValue * animationDurationValue = [userInfoobjectForKey: UIKeyboardAnimationDurationUserInfoKey];
NSTimeInterval animationDuration;
[AnimationDurationValue getValue: & animationDuration];
[Self moveInputBarWithKeyboardHeight: 0.0 withDuration: animationDuration];
}
# Pragma mark mobile view
-(Void) moveInputBarWithKeyboardHeight :( float) _ CGRectHeightwithDuration :( NSTimeInterval) _ NSTimeInterval {
CGRect rect = self. view. frame;
[UIView beginAnimations: nil context: NULL];
[UIView setAnimationDuration: _ NSTimeInterval];
Rect. origin. y =-_ CGRectHeight; // move the view up
Self. view. frame = rect;
[UIView commitAnimations];
}
-(Void) dealloc {
[[Nsicationicationcenter defacenter center] removeObserver: self]; // notification of removing Keyboard Events when the View Controller is eliminated
}
Hope to help you!
Zookeeper