我有一个 UIView
,无论 UIKeyboard
出现还是消失,我都想上下移动它。不幸的是我无法让它工作。
在 AutoLayout
之前,这是一个很简单的事情,但自从 AutoLayout
以来,我在这方面遇到了一些麻烦。
这是我目前所拥有的:
- (void)viewDidLoad {
[super viewDidLoad];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillShow:)
name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillHide:)
name:UIKeyboardWillHideNotification object:nil];
self.keyboardIsShown = NO;
}
-(void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
[[NSNotificationCenter defaultCenter] removeObserver:self
name:UIKeyboardWillShowNotification
object:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self
name:UIKeyboardWillHideNotification
object:nil];
}
-(void)keyboardWillShow:(NSNotification*)notification{
if (self.keyboardIsShown) {
return;
}
//change y Position of self.loginView
self.keyboardIsShown = YES;
}
-(void)keyboardWillHide:(NSNotification*)notification{
//Change y Position of self.loginView
self.keyboardIsShown = NO;
}
当然会调用这些方法,但我需要一些关于如何更改 UIView
的 y 位置的指导。仅更改框架的 y 位置根本不起作用。
在这里您可以找到我的界面设置以及我添加到 UIView 的约束。
所以除了 UIImageView
之外的所有组件都包含在 loginView Outlet
中。这是我想根据键盘向上或向下移动的 View 是否显示。
对我来说有点困难的是,正如您可能看到的那样,UIImageView 具有固定的高度、宽度并且垂直居中。
最佳答案
我刚刚完成了这个。基本上,将您需要移动的所有内容都包装在容器 View 中,然后与键盘显示/隐藏通知协作为顶部约束常量设置动画。
UIKeyboardWillShowNotification
UIKeyboardDidHideNotification
通知中有一个 userInfo,它会告诉您键盘的高度,还可以让您的动画与键盘同步以实现平滑过渡。
关于ios - 使用 AutoLayout 出现键盘时移动 UIView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30484700/