有個朋友問我,微信在聊天窗口有輸入文字的時候停蕉,返回到聊天列表頁愕鼓,再次進入這個帶草稿的聊天頁面,鍵盤是升起來的慧起,且輸入框已經在鍵盤上面(沒升起動畫)菇晃。各位自己可以用微信試試。
監(jiān)聽鍵盤高度改變的事件蚓挤,大家都很熟悉磺送,就是下面幾個。
UIKIT_EXTERN NSNotificationName const UIKeyboardWillShowNotification __TVOS_PROHIBITED;
UIKIT_EXTERN NSNotificationName const UIKeyboardDidShowNotification __TVOS_PROHIBITED;
UIKIT_EXTERN NSNotificationName const UIKeyboardWillHideNotification __TVOS_PROHIBITED;
UIKIT_EXTERN NSNotificationName const UIKeyboardDidHideNotification __TVOS_PROHIBITED;
監(jiān)聽這幾個通知灿意,我們就可以在鍵盤升起和下架的時候估灿,把輸入框始終跟隨鍵盤的頂部運動。
回到我們的問題缤剧,我想實現的是push的時候馅袁,鍵盤已經升起,且輸入框在鍵盤上面荒辕,二者都沒有升起的動畫汗销。
但是如果直接跟隨鍵盤高度去改變輸入框位置,你會發(fā)現在push聊天界面的時候抵窒,鍵盤倒是直接出現了大溜,但這個輸入框會有個從開始位置移動到鍵盤頂部的動畫。那么就猜測估脆,微信應該是一開始就將輸入框的位置設置在了鍵盤的頂部钦奋,然后再彈窗鍵盤。要實現這個的話疙赠,應該要保存上一次鍵盤的高度才行付材。因為你可以發(fā)現在viewWillAppear的時候,鍵盤高度還是0的圃阳。
當然可以做個驗證厌衔,先進入到微信,然后切換到A輸入法捍岳,輸入一些草稿文字之后富寿,退出聊天窗口睬隶。然后切換到短信,切換到另外一個鍵盤高度不一樣的輸入法B页徐,然后在切換回微信苏潜,進入剛剛的聊天界面,可以發(fā)現变勇,輸入框是有個從A輸入法的高度切換到B輸入法高度的動畫的恤左,時間很短,留意觀察搀绣。這樣就驗證了我們的猜測飞袋。
故要實現我朋友的這個需求,那么只需要記錄一下上一次鍵盤的高度就行了链患。
簡單的demo如下巧鸭,push出下面這個vc就可以測試了。
#import "VC2.h"
@interface VC2 ()
@property (strong, nonatomic) UITextField *textField;
@end
static CGFloat sKeyBoardHeight;
static NSString *sText;
@implementation VC2
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
_textField = [UITextField new];
_textField.text = sText;
_textField.placeholder = @"我是輸入框";
_textField.layer.borderColor = [UIColor grayColor].CGColor;
_textField.layer.borderWidth = 1;
[self.view addSubview:_textField];
if (sKeyBoardHeight > 0 && sText.length > 0) {
[self.textField becomeFirstResponder];
_textField.frame = CGRectMake(50, self.view.frame.size.height - sKeyBoardHeight - 100, 200, 100);
} else {
_textField.frame = CGRectMake(50, self.view.frame.size.height - 100, 200, 100);
}
UITapGestureRecognizer *recognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(backgroundTap)];
[self.view addGestureRecognizer:recognizer];
}
- (void)dealloc {
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
- (void)viewDidDisappear:(BOOL)animated {
sText = self.textField.text;
}
- (void)keyboardWillShow:(NSNotification *)notification {
NSValue *value = [[notification userInfo] objectForKey:UIKeyboardFrameEndUserInfoKey];
CGSize keyboardSize = [value CGRectValue].size;
CGFloat keyboradHeight = keyboardSize.height;
_textField.frame = CGRectMake(50, self.view.frame.size.height - keyboradHeight - 100, 200, 100);
NSLog(@"zzz keyboardWillShow %f", keyboradHeight);
sKeyBoardHeight = keyboradHeight;
}
- (void)keyboardWillHide:(NSNotification *)notification {
_textField.frame = CGRectMake(50, self.view.frame.size.height - 100, 200, 100);
}
- (void)backgroundTap {
[_textField resignFirstResponder];
}
@end