我有这个UITableView几乎填充我的整个UIViewController,并且我有一个UIView在底部包含一个按钮和一个文本框。UITableView和UIView与键盘将显示
当我点击文本框时,我想让UIView和tableview向上推,这样UIView就在键盘之上。
- UIView:
- UITextField
- UIButton
我已经在这里尝试了多个建议,但似乎没有在我的情况下工作。
我有这个UITableView几乎填充我的整个UIViewController,并且我有一个UIView在底部包含一个按钮和一个文本框。UITableView和UIView与键盘将显示
当我点击文本框时,我想让UIView和tableview向上推,这样UIView就在键盘之上。
- UIView:
- UITextField
- UIButton
我已经在这里尝试了多个建议,但似乎没有在我的情况下工作。
一个字:约束。
有我的文章在这里读: Height of iOS onscreen keyboard
它基本上有在屏幕底部的约束,每当用户打开屏幕键盘,它改变了这个constaint的高度。
希望这有助于。
正如评论中提到的,每个@IBOutlet
将您的底部约束(您的视图中包含文本字段和按钮的约束)连接到您的视图控制器。收听UIKeyboardWillHideNotification
和UIKeyboardWillShowNotification
并执行其选择器。当键盘出现时,将底部约束调整为键盘高度,并在隐藏时将其设置回0(或您在其中存储的任何值)。我会在动画中包装调整。
赞(在SWIFT):
func keyboardWillShow(notification: NSNotification) {
var info = notification.userInfo!
var keyboardFrame: CGRect = (info[UIKeyboardFrameEndUserInfoKey] as! NSValue).CGRectValue()
self.view.layoutIfNeeded()
UIView.animateWithDuration(0.25, animations: {() -> Void in
self.bottomConstraint.constant = keyboardFrame.size.height
self.view.layoutIfNeeded()
})
}
func keyboardWillHide(notification: NSNotification) {
self.view.layoutIfNeeded()
UIView.animateWithDuration(0.25, animations: {() -> Void in
self.bottomConstraint.constant = 0
self.view.layoutIfNeeded()
})
}
步骤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];
第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];
}];
}
解决方案在斯威夫特
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()
})
}
优秀的动画......非常好! –
超级解释谢谢:) –
很好的解释。 –
你尝试到出口增加至底部你的UIView的约束和更新当键盘显示 – Aladin
甚至更好的是调整sc的contentInsets按照Apple建议的键盘通知滚动查看。 https://developer.apple.com/library/ios/documentation/StringsTextFonts/Conceptual/TextAndWebiPhoneOS/KeyboardManagement/KeyboardManagement.html。检查部分**处理键盘通知**。 – GoGreen