如何调整的看法时,弹出键盘使用自动布局 [英] How do I resize views when keyboard pops up using Auto Layout
问题描述
我有一个观点(在这种情况下的UITableView
,但是这并不重要)我的的UIViewController
我希望有调整高度时,键盘弹出。
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
- Top space to superview: 0
- Trailing space to superview: 0
- Leading space to superview: 0
- Bottom space to superview: 0
Height equals = 424
我觉得这样做的最快捷的方式,是去除的高度和底部的空间限制,只是调整在code中的实际视图时 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.
我persume的的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];
}];
// ...
}
注:如果你的的 UITTableView
的不是在屏幕precisely底部,在的 contentInset
的值应该在这一行细化:的 [_的tableView setContentInset:UIEdgeInsetsMake(0.f ,0.f,_keyboardFrame.size.height,0.f);
这篇关于如何调整的看法时,弹出键盘使用自动布局的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!