使用自动布局弹出键盘时如何调整视图大小 [英] How do I resize views when keyboard pops up using Auto Layout
问题描述
我的 UIViewController
上有一个视图(在本例中为 UITableView
,但这并不重要),我希望在键盘弹出时调整高度.
I have a view (a UITableView
in this case, but that's not important) on my UIViewController
that I want to have the height resized when the keyboard pops up.
在自动布局中执行此操作的最佳方法是什么?目前我认为我有这些限制:
What is the best way to do this in Auto Layout? Currently on the view I have these constraints:
- 超级视图的顶部空间:0
- 超级视图的尾随空间:0
- 超视图的领先空间:0
- 超级视图的底部空间:0
高度等于 = 424
我认为这样做的最快方法是删除高度和底部空间约束,并在调用 keyboardDidAppear
通知时调整代码中实际视图的大小,但还有其他方法吗?这样做吗?
I think the quickest way of doing this, is to remove the height and bottom space constraint and just resize the actual view in the code when keyboardDidAppear
notification is called, but is there any other ways to do this?
我删除了高度限制,我的错.
I removed the height constraint, my bad.
推荐答案
我给你一个通用的想法,可能需要根据你的实际项目重新调整.
I would give you a generic idea, it may need to be re-adjusted for your actual project.
let notificationTokenKeyboardWillAppear = NotificationCenter.default.addObserver(forName: UIResponder.keyboardWillShowNotification, object: nil, queue: nil) { (note) in
guard let keyboardFrame = (note.userInfo?[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue else { return }
UIView.animate(withDuration: CATransaction.animationDuration(), animations: {
self.scrollView?.contentInset = UIEdgeInsets(top: 0.0, left: 0.0, bottom: keyboardFrame.size.height, right: 0.0)
}, completion: nil)
}
和
let notificationTokenKeyboardWillHide = NotificationCenter.default.addObserver(forName: UIResponder.keyboardWillHideNotification, object: nil, queue: nil) { (_) in
UIView.animate(withDuration: CATransaction.animationDuration(), animations: {
self.scrollView?.contentInset = .zero
}, completion: nil)
}
注意-1: scrollView
在这里表示 UIScrollView
的任何子集,例如UITableView
或 UICollectionView
等...
NOTE-1: scrollView
represents here any subset of UIScrollView
, e.g. UITableView
or UICollectionView
, etc...
注意-2:您需要通过调用removeObserver(_:)
方法,当您将要释放视图并且不再需要基于闭包的观察者时
我认为 UITableView *_tableView
之前已经在某处正确设置.
I persume the UITableView *_tableView
has been set up properly somewhere before.
- (void)viewDidLoad {
// ...
[[NSNotificationCenter defaultCenter] addObserverForName:UIKeyboardWillShowNotification object:nil queue:nil usingBlock:^(NSNotification *note) {
id _obj = [note.userInfo valueForKey:UIKeyboardFrameEndUserInfoKey];
CGRect _keyboardFrame = CGRectNull;
if ([_obj respondsToSelector:@selector(getValue:)]) [_obj getValue:&_keyboardFrame];
[UIView animateWithDuration:0.25f delay:0.f options:UIViewAnimationOptionCurveEaseInOut animations:^{
[_tableView setContentInset:UIEdgeInsetsMake(0.f, 0.f, _keyboardFrame.size.height, 0.f)];
} completion:nil];
}];
[[NSNotificationCenter defaultCenter] addObserverForName:UIKeyboardWillHideNotification object:nil queue:nil usingBlock:^(NSNotification *note) {
[UIView animateWithDuration:0.25f delay:0.f options:UIViewAnimationOptionCurveEaseInOut animations:^{
[_tableView setContentInset:UIEdgeInsetsZero];
} completion:nil];
}];
// ...
}
注意:如果您的 UITableView
没有准确地位于屏幕底部, contentInset
值应该在这一行细化: [_tableView setContentInset:UIEdgeInsetsMake(0.f, 0.f, _keyboardFrame.size.height, 0.f)];代码>
这篇关于使用自动布局弹出键盘时如何调整视图大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!