使用UIKeyboard预测打开和关闭管理视图位置
我想在键盘显示时向上移动视图,在键盘被隐藏时向下移动,就像在消息中一样。 我能够实现它,但我隐藏预测有一些空白。
我用“ UIKeyboardWillShowNotification ”和“ UIKeyboardWillHideNotification ”来处理键盘的运动
- (void)viewWillAppear:(BOOL)animated { [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil]; } - (void)viewWillDisappear:(BOOL)animated { [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil]; [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil]; } #pragma mark - keyboard movements - (void)keyboardWillShow:(NSNotification *)notification { CGSize keyboardSize = [[[notification userInfo] objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size; [UIView animateWithDuration:0.3 animations:^{ CGRect f = self.view.frame; f.origin.y = -keyboardSize.height; self.view.frame = f; }]; } -(void)keyboardWillHide:(NSNotification *)notification { [UIView animateWithDuration:0.3 animations:^{ CGRect f = self.view.frame; f.origin.y = 0.0f; self.view.frame = f; }]; }
我想处理视图的移动,就像在消息应用程序中发生的那样。
CGSize keyboardSize = [[[notification userInfo] objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
用以下线代替
CGSize keyboardSize = [[[notification userInfo] objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size;
这是我的意识:
- (void)viewDidLoad { [super viewDidLoad]; self.defaultViewFrame = self.view.frame; } - (void)viewWillAppear:(BOOL)animated { [super viewWillAppear: animated]; [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(onKeyboardShow:) name:UIKeyboardWillShowNotification object:nil]; [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(onKeyboardHide:) name:UIKeyboardWillHideNotification object:nil]; } - (void)viewWillDisappear:(BOOL)animated { [super viewWillDisappear:animated]; [[NSNotificationCenter defaultCenter] removeObserver:self]; } - (void)onKeyboardShow:(NSNotification *)notification { // method when keyboard appears CGFloat keyboardHeight = [notification.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue].size.height; [self updateView:YES withKeyboardHeight:keyboardHeight]; } - (void)onKeyboardHide:(NSNotification *)notification { // method when keyboard hides CGFloat keyboardHeight = [notification.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue].size.height; [self updateView:NO withKeyboardHeight:keyboardHeight]; } ***************************** - (void)updateView:(BOOL)show withKeyboardHeight:(CGFloat)height{ [self.view setFrame:self.defaultViewFrame]; CGRect newViewRect = CGRectMake(self.view.frame.origin.x, self.view.frame.origin.y - height, self.view.frame.size.width, self.view.frame.size.height); if (show) { [UIView animateWithDuration:0.3 animations:^{ [self.view setFrame:newViewRect]; } completion:^(BOOL finished) {}]; } }