Mar*_*y W 33 objective-c uikeyboard ios autolayout
我试图平滑地制作一个具有自动布局约束的桌面视图.我在我的.h中引用了约束"keyboardHeight",并在IB中将其链接起来.我想要做的就是在弹出窗口时用键盘为表格视图设置动画.这是我的代码:
- (void)keyboardWillShow:(NSNotification *)notification
{
NSDictionary *info = [notification userInfo];
NSValue *kbFrame = [info objectForKey:UIKeyboardFrameEndUserInfoKey];
NSTimeInterval animationDuration = [[info objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue];
CGRect keyboardFrame = [kbFrame CGRectValue];
CGFloat height = keyboardFrame.size.height;
[UIView animateWithDuration:animationDuration animations:^{
self.keyboardHeight.constant = -height;
[self.view setNeedsLayout];
}];
}
Run Code Online (Sandbox Code Playgroud)
事情是动画块是瞬时的,我看到在键盘完成动画之前出现了空格.所以基本上我看到视图的白色背景,因为键盘是动画.只要键盘是动画,我就无法使动画持续.
我接近这个错误的方式吗?提前致谢!
Joh*_*pia 52
试试这种方式:
self.keyboardHeight.constant = -height;
[self.view setNeedsUpdateConstraints];
[UIView animateWithDuration:animationDuration animations:^{
[self.view layoutIfNeeded];
}];
Run Code Online (Sandbox Code Playgroud)
记住这种模式,因为这应该是更新基于约束的布局的正确方法(根据WWDC).NSLayoutConstraint只要您打电话setNeedsUpdateConstraints,您也可以添加或删除s .
Jam*_*ang 14
如果您正在使用UITableViewController,iOS应自动调整键盘大小以调整contentInsets.但是如果你的tableView在UIViewController中,你可能想要使用它:
KeyboardLayoutConstraint在春天框架.到目前为止我找到的最简单的解决方案.

尝试下一个代码.在这种情况下,表格视图位于屏幕的底部边缘.
- (void)keyboardWillShow:(NSNotification *)notification { // UIKeyboardWillShowNotification
NSDictionary *info = [notification userInfo];
NSValue *keyboardFrameValue = [info objectForKey:UIKeyboardFrameEndUserInfoKey];
NSTimeInterval animationDuration = [[info objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue];
CGRect keyboardFrame = [keyboardFrameValue CGRectValue];
BOOL isPortrait = UIDeviceOrientationIsPortrait([UIApplication sharedApplication].statusBarOrientation);
CGFloat keyboardHeight = isPortrait ? keyboardFrame.size.height : keyboardFrame.size.width;
// constrBottom is a constraint defining distance between bottom edge of tableView and bottom edge of its superview
constrBottom.constant = keyboardHeight;
// or constrBottom.constant = -keyboardHeight - in case if you create constrBottom in code (NSLayoutConstraint constraintWithItem:...:toItem:...) and set views in inverted order
[UIView animateWithDuration:animationDuration animations:^{
[tableView layoutIfNeeded];
}];
}
- (void)keyboardWillHide:(NSNotification *)notification { // UIKeyboardWillHideNotification
NSDictionary *info = [notification userInfo];
NSTimeInterval animationDuration = [[info objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue];
constrBottom.constant = 0;
[UIView animateWithDuration:animationDuration animations:^{
[tableView layoutIfNeeded];
}];
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
18096 次 |
| 最近记录: |