我有一个 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 坐标的指引。仅仅改变 frame 的 y 坐标是无效的。在这里,你可以找到我的界面设置和我添加到 UIView 的约束条件。 所以除了
UIImageView
外,所有的组件都包含在 loginView Outlet
中。这是我想要根据键盘是否显示而向上或向下移动的视图。对我来说有点困难的是,正如你可能看到的,UIImageView 有一个固定的高度、宽度和垂直居中。
- (void)viewWillLayoutSubviews
。 - 0yeoj