標籤:
最近做的項目中,有一個類似微博中的評論轉寄功能,螢幕底端有一個輸入框用textView來做,當textView成為第一響應者的時候它的Y值隨著鍵盤高度的改變而改變,保證textView緊貼著鍵盤,但又不會被鍵盤擋住。
下面是我實現的方法:(利用通知)
| 123456789101112 |
// 鍵盤通知 // 鍵盤的frame發生改變時發出的通知(位置和尺寸) // UIKeyboardWillChangeFrameNotification // UIKeyboardDidChangeFrameNotification // 鍵盤顯示時發出的通知 // UIKeyboardWillShowNotification // UIKeyboardDidShowNotification // 鍵盤隱藏時發出的通知 // UIKeyboardWillHideNotification // UIKeyboardDidHideNotification [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillChangeFrame:) name:UIKeyboardWillChangeFrameNotification object:nil];//在這裡註冊通知 |
下面是監聽通知:
| 12345678910111213141516171819202122232425262728293031323334353637 |
#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; // 動畫的期間 doubleduration = [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;//這裡的<span style="">self.toolbar就是我的輸入框。</span> }else{ self.toolbar.y = keyboardF.origin.y - self.toolbar.height; } }];} |
當然,這裡我為UIView寫了一個類別,實現如下:
.h檔案中聲明
| 12345678910 |
@interfaceUIView (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)
| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798 |
@implementationUIView (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{ returnself.frame.origin.x;} - (CGFloat)y{ returnself.frame.origin.y;} - (void)setCenterX:(CGFloat)centerX{ CGPoint center = self.center; center.x = centerX; self.center = center;} - (CGFloat)centerX{ returnself.center.x;} - (void)setCenterY:(CGFloat)centerY{ CGPoint center = self.center; center.y = centerY; self.center = center;} - (CGFloat)centerY{ returnself.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{ returnself.frame.size.height;} - (CGFloat)width{ returnself.frame.size.width;} - (void)setSize:(CGSize)size{ CGRect frame = self.frame; frame.size = size; self.frame = frame;} - (CGSize)size{ returnself.frame.size;} - (void)setOrigin:(CGPoint)origin{ CGRect frame = self.frame; frame.origin = origin; self.frame = frame;} - (CGPoint)origin{ returnself.frame.origin;}@end |
有需要的朋友可以直接用
iOS開發之監聽鍵盤高度的變化