iOS開發之監聽鍵盤高度的變化
最近做的項目中,有一個類似微博中的評論轉寄功能,螢幕底端有一個輸入框用textView來做,當textView成為第一響應者的時候它的Y值隨著鍵盤高度的改變而改變,保證textView緊貼著鍵盤,但又不會被鍵盤擋住。
下面是我實現的方法:(利用通知)
// 鍵盤通知 // 鍵盤的frame發生改變時發出的通知(位置和尺寸) // UIKeyboardWillChangeFrameNotification // UIKeyboardDidChangeFrameNotification // 鍵盤顯示時發出的通知 // UIKeyboardWillShowNotification // UIKeyboardDidShowNotification // 鍵盤隱藏時發出的通知 // UIKeyboardWillHideNotification // UIKeyboardDidHideNotification [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillChangeFrame:) name:UIKeyboardWillChangeFrameNotification object:nil];//在這裡註冊通知
下面是監聽通知:
#pragma mark - 監聽方法/** * 鍵盤的frame發生改變時調用(顯示、隱藏等) */- (void)keyboardWillChangeFrame:(NSNotification *)notification{ // if (self.picking) return; /** notification.userInfo = @{ // 鍵盤彈出\隱藏後的frame UIKeyboardFrameEndUserInfoKey = NSRect: {{0, 352}, {320, 216}}, // 鍵盤彈出\隱藏所耗費的時間 UIKeyboardAnimationDurationUserInfoKey = 0.25, // 鍵盤彈出\隱藏動畫的執行節奏(先快後慢,勻速) UIKeyboardAnimationCurveUserInfoKey = 7 } */ NSDictionary *userInfo = notification.userInfo; // 動畫的期間 double duration = [userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue]; // 鍵盤的frame CGRect keyboardF = [userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue]; // 執行動畫 [UIView animateWithDuration:duration animations:^{ // 工具條的Y值 == 鍵盤的Y值 - 工具條的高度 if (keyboardF.origin.y > self.view.height) { // 鍵盤的Y值已經遠遠超過了控制器view的高度 self.toolbar.y = self.view.height - self.toolbar.height;//這裡的self.toolbar就是我的輸入框。 } else { self.toolbar.y = keyboardF.origin.y - self.toolbar.height; } }];}
當然,這裡我為UIView寫了一個類別,實現如下:
.h檔案中聲明
@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檔案中實現(重寫setter 和 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
有需要的同行可以直接拿來用