如何使用whatsapp样式,消息空间在单击时会从底部向上推动键盘,同时也可以向上推动工具栏。然后当取消(即在后台点击)时,它会用工具栏将键盘向下推
答案 0 :(得分:2)
这是通过U IKeyboardWillShowNotification
,UIKeyboardDidShowNotification
,UIKeyboardWillHideNotification
和UIKeyboardDidHideNotification
通知来完成的。
然后,当您处理通知时,您可以调整框架的高度:
例如:
- (void) keyboardWillShow:(NSNotification *)aNotification{
NSDictionary* info = [aNotification userInfo];
NSTimeInterval duration = [[info objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue];
NSValue* aValue = [info objectForKey:UIKeyboardFrameBeginUserInfoKey];
CGFloat keyboardHeight = [aValue CGRectValue].size.height;
self.keyboardVissible = YES;
[UIView animateWithDuration:duration animations:^{
CGRect frame = self.contentView.frame;
frame.size.height -= keyboardHeight;
self.contentView.frame = frame;
}];
}
您需要注册才能收到通知,您应该只在视图可见时收听键盘通知,如果您在viewDidLoad
中执行此操作会发生奇怪的事情:
- (void) viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
}
- (void) viewDidDisappear:(BOOL)animated {
[super viewDidDisappear:animated];
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];
}