当它出现在屏幕上时,如何让键盘向上推动视图?Xcode Swift
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/35561977/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I make a keyboard push the view up when it comes on screen? Xcode Swift
提问by Austin-Michael Komatz
So I have a view with a text field in it, but the text field is at the bottom. When I click the keyboard, it pops up and covers the text field, this is my problem. I was wondering if it is at all possible to push the rest of the view up when a keyboard comes up?
所以我有一个带有文本字段的视图,但文本字段位于底部。当我单击键盘时,它会弹出并覆盖文本字段,这是我的问题。我想知道当键盘出现时是否有可能将其余的视图向上推?
回答by Ahmed Onawale
You can register your view controller to be notified when the keyboard is about to be shown, then you push your view up.
您可以注册您的视图控制器,以便在即将显示键盘时收到通知,然后向上推视图。
class ViewController: UIViewController {
var keyboardAdjusted = false
var lastKeyboardOffset: CGFloat = 0.0
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
NSNotificationCenter.defaultCenter().addObserver(self, selector: "keyboardWillShow:", name: UIKeyboardWillShowNotification, object: nil)
NSNotificationCenter.defaultCenter().addObserver(self, selector: "keyboardWillHide:", name: UIKeyboardWillHideNotification, object: nil)
}
override func viewDidDisappear(animated: Bool) {
super.viewDidDisappear(animated)
NSNotificationCenter.defaultCenter().removeObserver(self, name: UIKeyboardWillShowNotification, object: nil)
NSNotificationCenter.defaultCenter().removeObserver(self, name: UIKeyboardWillHideNotification, object: nil)
}
func keyboardWillShow(notification: NSNotification) {
if keyboardAdjusted == false {
lastKeyboardOffset = getKeyboardHeight(notification)
view.frame.origin.y -= lastKeyboardOffset
keyboardAdjusted = true
}
}
func keyboardWillHide(notification: NSNotification) {
if keyboardAdjusted == true {
view.frame.origin.y += lastKeyboardOffset
keyboardAdjusted = false
}
}
func getKeyboardHeight(notification: NSNotification) -> CGFloat {
let userInfo = notification.userInfo
let keyboardSize = userInfo![UIKeyboardFrameEndUserInfoKey] as! NSValue
return keyboardSize.CGRectValue().height
}
}
回答by Evgeny Sureev
You should put text field into UIScrollView
and adjust its contentInset
on UIKeyboardDidShowNotification
.
您应该将文本字段放入UIScrollView
并调整其contentInset
上UIKeyboardDidShowNotification
。
Read this help page: https://developer.apple.com/library/ios/documentation/StringsTextFonts/Conceptual/TextAndWebiPhoneOS/KeyboardManagement/KeyboardManagement.html#//apple_ref/doc/uid/TP40009542-CH5-SW7
阅读此帮助页面:https: //developer.apple.com/library/ios/documentation/StringsTextFonts/Conceptual/TextAndWebiPhoneOS/KeyboardManagement/KeyboardManagement.html#//apple_ref/doc/uid/TP40009542-CH5-SW7
回答by Omar Faruqe
You can check this tutorial Move UITextField so keyboard does not hide it
您可以查看本教程移动 UITextField 以便键盘不会隐藏它
You will find demo for this here
你会在这里找到演示
Or you can check the answer of this stackoverflow Move textfield when keyboard appears swift
或者您可以在键盘出现 swift 时检查此 stackoverflow Move 文本字段的答案
Best answer there is to manage by updating bottom constraint like this
最好的答案是通过更新这样的底部约束来管理
func keyboardWasShown(notification: NSNotification) {
var info = notification.userInfo!
var keyboardFrame: CGRect = (info[UIKeyboardFrameEndUserInfoKey] as! NSValue).CGRectValue()
UIView.animateWithDuration(0.1, animations: { () -> Void in
self.bottomConstraint.constant = keyboardFrame.size.height + 20
})
}