在UIViewController中,我有几个文本字段,一个按钮位于UIViewController的底部.对于按钮,我设置了一个常量为0的底部约束.然后我从底部约束到UIViewController创建了一个出口.
当我运行我的代码时,按钮不会向上移动.我已经看到了关于stackoverflow的建议,我应该添加UIScrollView,但这意味着,我必须删除UIViewController上的所有对象,放置UIScrollView,然后再次将我的对象放在UIVIewController上.
@IBOutlet weak var bottomConstraint: NSLayoutConstraint!
// When tapping outside of the keyboard, close the keyboard down
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
self.view.endEditing(true)
}
// Stop Editing on Return Key Tap. textField parameter refers to any textfield within the view
func textFieldShouldReturn(textField: UITextField) -> Bool {
textField.resignFirstResponder()
return true
}
// when keyboard is about to show assign the height of the keyboard to bottomConstraint.constant of our button so that it will move up
func keyboardWillShow(notification: NSNotification) {
if let userInfo = notification.userInfo {
if let keyboardSize: CGRect = (userInfo[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.CGRectValue() {
bottomConstraint.constant = keyboardSize.size.height
view.setNeedsLayout()
}
}
}
// When keyboard is hidden, move the button to the bottom of the view
func keyboardWillHide(notification: NSNotification) {
bottomConstraint.constant = 0.0
view.setNeedsLayout()
}
Run Code Online (Sandbox Code Playgroud)
解决这个问题的典型方法是使用以下代码移动键盘:
在ViewController类中:
func keyboardWillShow(notification: NSNotification) {
if let keyboardSize = (notification.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.CGRectValue() {
if view.frame.origin.y == 0{
let height = keyboardSize.height
self.view.frame.origin.y += height
}
}
}
func keyboardWillHide(notification: NSNotification) {
if let keyboardSize = (notification.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.CGRectValue() {
if view.frame.origin.y != 0 {
let height = keyboardSize.height
self.view.frame.origin.y -= height
}
}
}
Run Code Online (Sandbox Code Playgroud)
在ViewDidLoad方法中:
NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("keyboardWillShow:"), name: UIKeyboardWillShowNotification, object: nil)
NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("keyboardWillHide:"), name: UIKeyboardWillHideNotification, object: nil)
Run Code Online (Sandbox Code Playgroud)
请阅读:
不允许您尝试解决问题的方式.在上面的代码中,如果您更改view为按钮变量名称,按钮将会向上拍摄然后再向下.这是因为自动布局和程序布局不能一起工作,它是一个或另一个.解决这个问题的方法是通过编程方式创建该按钮(使用CGRect),然后使用上面的代码在键盘按下时仅移动该按钮.(通过更改view为按钮变量名称来执行此操作.
func keyboardWillShow(notification: NSNotification) {
if let keyboardSize = (notification.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.CGRectValue() {
if view.frame.origin.y == 0{
let height = keyboardSize.height
self.myButton.frame.origin.y += height
}
}
}
func keyboardWillHide(notification: NSNotification) {
if let keyboardSize = (notification.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.CGRectValue() {
if view.frame.origin.y != 0 {
let height = keyboardSize.height
self.myButton.frame.origin.y -= height
}
}
}
Run Code Online (Sandbox Code Playgroud)
要以编程方式创建按钮,您将使用与此类似的代码:
myButton.frame = CGRect(...)
Run Code Online (Sandbox Code Playgroud)
对 Ryan 上面的回答的补充是,这可以通过自动布局来完成,不需要框架和 CGRect。
雨燕5
在您看来,像平常一样约束按钮,但添加对约束的引用,以便在键盘隐藏/显示时进行修改:
var bottomButtonConstraint = NSLayoutConstraint()
bottomButtonConstraint = yourButton.bottomAnchor.constraint(equalTo: safeAreaLayoutGuide.bottomAnchor, constant: -12)
bottomButtonConstraint.isActive = true
Run Code Online (Sandbox Code Playgroud)
在你的 ViewController 中viewDidLoad():
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow(notification:)), name: UIResponder.keyboardWillShowNotification, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillHide(notification:)), name: UIResponder.keyboardWillHideNotification, object: nil)
Run Code Online (Sandbox Code Playgroud)
同样在你的 ViewController 中:
@objc private func keyboardWillShow(notification: NSNotification) {
if let keyboardSize = (notification.userInfo?[UIResponder.keyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue {
self.yourCustomView.bottomButtonConstraint.constant -= keyboardSize.height
}
}
@objc private func keyboardWillHide(notification: NSNotification) {
self.yourCustomView.bottomButtonConstraint.constant = -12
}
Run Code Online (Sandbox Code Playgroud)