IOS development-listening for changes in keyboard height
In a recent project, there is a comment forwarding function similar to that in Weibo. At the bottom of the screen, there is an input box that uses textView, when textView becomes the first responder, its Y value changes with the keyboard height, ensuring that textView is closely bound to the keyboard, but not blocked by the keyboard.
The following is my Implementation Method: (using notifications)
// Keyboard notification // the notification (location and size) issued when the frame of the keyboard changes) // notify // notification sent when the keyboard is displayed // UIKeyboardWillShowNotification // UIKeyboardDidShowNotification // notification sent when the keyboard is hidden // notify [[nsicationicationcenter defadefa] addObserver: self selector: @ selector (keyboardWillChangeFrame :) name: UIKeyboardWillChangeFrameNotification object: nil]; // register a notification here
The following is a listener notification:
# Pragma mark-listener method/*** call (display, hide, etc.) when the frame of the keyboard changes */-(void) keyboardWillChangeFrame :( NSNotification *) notification {// if (self. picking) return;/** notification. userInfo ={ // The keyboard pops up \ The Hidden frame UIKeyboardFrameEndUserInfoKey = NSRect: {0,352 },{ 320,216 }}, // keyboard pop-up \ hide time consumed UIKeyboardAnimationDurationUserInfoKey = 0.25, // keyboard pop-up \ hide the animation execution speed (Fast first, slow, uniform speed) UIKeyboardAnimationCurveUserInfoKey = 7} */NSDictio Nary * userInfo = notification. userInfo; // animation duration double duration = [userInfo [UIKeyboardAnimationDurationUserInfoKey] doubleValue]; // The frame CGRect keyboardF of the keyboard = [userInfo [UIKeyboardFrameEndUserInfoKey] CGRectValue]; // execute the animation [UIView animateWithDuration: duration animations: ^ {// Y value of the toolbar = Y value of the keyboard-height of the toolbar if (keyboardF. origin. y> self. view. height) {// The Y value of the keyboard has far exceeded the height of the controller view self. toolbar. y = self. v Iew. height-self. toolbar. height; // here, self. toolbar is my input box.} Else {self. toolbar. y = keyboardF. origin. y-self. toolbar. height;}];}
Of course, here I write a category for UIView, which is implemented as follows:
. H file Declaration
@interface UIView (Extension)@property (nonatomic, assign) CGFloat x;@property (nonatomic, assign) CGFloat y;@property (nonatomic, assign) CGFloat width;@property (nonatomic, assign) CGFloat height;@property (nonatomic, assign) CGFloat centerX;@property (nonatomic, assign) CGFloat centerY;@property (nonatomic, assign) CGSize size;@property (nonatomic, assign) CGPoint origin;@end
. M file implementation (rewrite setter and getter)
@implementation UIView (Extension)- (void)setX:(CGFloat)x{ CGRect frame = self.frame; frame.origin.x = x; self.frame = frame;}- (void)setY:(CGFloat)y{ CGRect frame = self.frame; frame.origin.y = y; self.frame = frame;}- (CGFloat)x{ return self.frame.origin.x;}- (CGFloat)y{ return self.frame.origin.y;}- (void)setCenterX:(CGFloat)centerX{ CGPoint center = self.center; center.x = centerX; self.center = center;}- (CGFloat)centerX{ return self.center.x;}- (void)setCenterY:(CGFloat)centerY{ CGPoint center = self.center; center.y = centerY; self.center = center;}- (CGFloat)centerY{ return self.center.y;}- (void)setWidth:(CGFloat)width{ CGRect frame = self.frame; frame.size.width = width; self.frame = frame;}- (void)setHeight:(CGFloat)height{ CGRect frame = self.frame; frame.size.height = height; self.frame = frame;}- (CGFloat)height{ return self.frame.size.height;}- (CGFloat)width{ return self.frame.size.width;}- (void)setSize:(CGSize)size{ CGRect frame = self.frame; frame.size = size; self.frame = frame;}- (CGSize)size{ return self.frame.size;}- (void)setOrigin:(CGPoint)origin{ CGRect frame = self.frame; frame.origin = origin; self.frame = frame;}- (CGPoint)origin{ return self.frame.origin;}@end
Can be used directly by peers in need