我有一个UIView,屏幕底部放置了UITextField
,当键盘出现时我想向上移动。
我添加了keyboardFrameDidChange
观察员,以便在键盘出现/消失时收到通知。这是方法:
-(void)keyboardFrameDidChange:(NSNotification*)notification{
NSDictionary* info = [notification userInfo];
CGRect kKeyBoardFrame = [[info objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
[bottomView layoutIfNeeded];
[UIView animateWithDuration:[notification.userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue] delay:0 options:[[[notification userInfo] objectForKey:UIKeyboardAnimationCurveUserInfoKey] integerValue] animations:^{
[bottomView setFrame:CGRectMake(0, kKeyBoardFrame.origin.y - bottomView.frame.size.height, 320, bottomView.frame.size.height)];
} completion:^(BOOL finished) {
[bottomView layoutIfNeeded];
NSLog(@"frame ** %f",self.bottomView.frame.origin.y);
}];
}
此处frame of bottomView
正在发生变化,但UI中没有任何变化。 bottomView
只是保持在最底层。可能是什么原因?
答案 0 :(得分:1)
这听起来像是一个自动布局问题。如果启用了自动布局,则应通过调整其约束而不是设置框架来更改视图的位置。将IBOutlet设置为文本字段和superview底部之间的约束(在我的示例中为bottomCon),然后进行调整。
-(void)keyboardFrameDidChange:(NSNotification*)notification{
NSDictionary* info = [notification userInfo];
CGRect kKeyBoardFrame = [[info objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
[_bottomView layoutIfNeeded];
[UIView animateWithDuration:[notification.userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue] delay:0 options:[[[notification userInfo] objectForKey:UIKeyboardAnimationCurveUserInfoKey] integerValue] animations:^{
self.bottomCon.constant = kKeyBoardFrame.origin.y;
[_bottomView layoutIfNeeded];
} completion:^(BOOL finished) {
NSLog(@"frame ** %f",self.bottomView.frame.origin.y);
}];
}