gpt4 book ai didi

swift - 键盘快速出现时移动按钮

转载 作者:搜寻专家 更新时间:2023-10-31 08:15:15 24 4
gpt4 key购买 nike

在 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()
}

enter image description here

最佳答案

解决这个问题的典型方法是使用如下代码移动键盘:

在 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
}

}
}

在 ViewDidLoad 方法中:

  NotificationCenter.default.addObserver(self, selector: Selector("keyboardWillShow:"), name: NSNotification.Name.UIKeyboardWillShow, object: nil)
NotificationCenter.default.addObserver(self, selector: Selector("keyboardWillHide:"), name: NSNotification.Name.UIKeyboardWillHide, object: nil)

请阅读此内容:不允许您尝试解决问题的方式。在上面的代码中,如果您将 view 更改为您的按钮变量名称,按钮将弹起然后落回。这是因为 Auto Layout 和 Programmatic layout 不能一起工作,它是一个或另一个。解决此问题的方法是通过编程方式创建该按钮(使用 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.yourBtn.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.yourBtn.frame.origin.y -= height
}
}
}

要以编程方式创建按钮,您将使用与此类似的代码:

myButton.frame = CGRect(...)

关于swift - 键盘快速出现时移动按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39253656/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com