Mad*_*ard 2 iphone xcode objective-c uitableview ios
我有这个UITableView几乎填满了我的整个UIViewController,我在底部有一个包含按钮和文本字段的UIView.
当我单击文本字段时,我希望UIView和tableview向上推,这样UIView就在键盘顶部.
- UIView:
- UITextField
- UIButton
Run Code Online (Sandbox Code Playgroud)
我在这里尝试了多个建议,但似乎没有一个在我的情况下有用.
EI *_*2.0 16
第1步:
制作底部约束的出口UIView

步骤2:
添加键盘显示和隐藏的观察者,然后根据键盘高度更改约束常量.
//**In viewDidLoad method**
// register for keyboard notifications
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillShow:)
name:UIKeyboardWillShowNotification
object:nil];
// register for keyboard notifications
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillHide:)
name:UIKeyboardWillHideNotification
object:nil];
Run Code Online (Sandbox Code Playgroud)
第3步:
管理约束作为键盘显示和隐藏通知,如下所示
- (void)keyboardWillShow:(NSNotification *)notification
{
NSDictionary* userInfo = [notification userInfo];
// get the size of the keyboard
CGSize keyboardSize = [[userInfo objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
CGSize keyboardSizeNew = [[userInfo objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size;
[UIView animateWithDuration:0.2
animations:^{
_bottomConstraintofView.constant = keyboardSizeNew.height;
[self.view layoutIfNeeded]; // Called on parent view
}];
}
- (void)keyboardWillHide:(NSNotification *)notification
{
[UIView animateWithDuration:0.2
animations:^{
_bottomConstraintofView.constant = 0;
[self.view layoutIfNeeded];
}];
}
Run Code Online (Sandbox Code Playgroud)
Swift中的解决方案
func keyboardWillShow(notification: NSNotification){
let userInfo:NSDictionary = notification.userInfo!
let keyboardSize:CGSize = userInfo.objectForKey(UIKeyboardFrameBeginUserInfoKey)!.CGRectValue().size
let keyboardSizeNow:CGSize = userInfo.objectForKey(UIKeyboardFrameEndUserInfoKey)!.CGRectValue().size
UIView.animateWithDuration(0.2, animations: { () -> Void in
self.bottomConstraintofView.constant = keyboardSizeNow.height
self.view.layoutIfNeeded()
})
}
func keyboardWillHide(notification: NSNotification){
UIView.animateWithDuration(0.2, animations: { () -> Void in
self.bottomConstraintofView.constant = 0
self.view.layoutIfNeeded()
})
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1777 次 |
| 最近记录: |