Xcode 将UIView底部空间约束设置为键盘高度
我是autolayout的忠实粉丝,但我一直在故事板中实现它 我有一个UIView,我希望底部空间约束位于键盘结束的位置 我已经定义了所有约束,有人能告诉我如何在代码中实现这个约束吗?以及如何根据特定的iDevice获取键盘高度值,并将其与约束匹配Xcode 将UIView底部空间约束设置为键盘高度,xcode,autolayout,Xcode,Autolayout,我是autolayout的忠实粉丝,但我一直在故事板中实现它 我有一个UIView,我希望底部空间约束位于键盘结束的位置 我已经定义了所有约束,有人能告诉我如何在代码中实现这个约束吗?以及如何根据特定的iDevice获取键盘高度值,并将其与约束匹配 谢谢像这样走出视野的底部限制 @property (weak, nonatomic) IBOutlet NSLayoutConstraint *constraintContainerBottom; 用这个 - (void)viewWillAppea
谢谢像这样走出视野的底部限制
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *constraintContainerBottom;
用这个
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[self setupKeyboard:YES];
}
- (void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
[self setupKeyboard:NO];
}
- (void)setupKeyboard:(BOOL)appearing
{
if (appearing) {
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
}else{
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];
}
}
- (void)keyboardWillShow:(NSNotification*)notification
{
CGSize keyboardSize = [[[notification userInfo] objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size;
double duration = [[[notification userInfo] objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue];
int curve = [[[notification userInfo] objectForKey:UIKeyboardAnimationCurveUserInfoKey] intValue];
[UIView beginAnimations:@"keyboardShown" context:nil];
[UIView setAnimationDuration:duration];
[UIView setAnimationCurve:curve];
self.constraintContainerBottom.constant = keyboardSize.height;
[self.view layoutIfNeeded];
[UIView setAnimationDelegate:self];
[UIView commitAnimations];
}
- (void)keyboardWillHide:(NSNotification*)notification
{
double duration = [[[notification userInfo] objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue];
int curve = [[[notification userInfo] objectForKey:UIKeyboardAnimationCurveUserInfoKey] intValue];
[UIView beginAnimations:@"keyboardHidden" context:nil];
[UIView setAnimationDuration:duration];
[UIView setAnimationCurve:curve];
self.constraintContainerBottom.constant = 0;
[self.view layoutIfNeeded];
[UIView setAnimationDelegate:self];
[UIView commitAnimations];
}
@Akshay Kheveria链接没有意义。这里重复了。“constraint”不是NSLayoutConstraint IBOutlet的属性…?@nickjf89它取决于您的处理方式。它的解决方案很好。:)我认为这是可行的。它实际上是恒定的……我已经更新了答案。对不起,如果我太笨了……我如何将约束的IBOutlet设置为我的UIView?只需双击约束,然后在左窗格中选择一个约束。只需将其拖动到所需的类文件并设置名称。