我的swift
应用程序中有一个按钮,位于屏幕底部。限制条件是:
我还附加了用于约束的插座,该约束将我的按钮与屏幕底部分开:
当我运行该应用程序时,我看到我的按钮(我添加了一些背景色,以便使我的示例清晰可见):
现在,发生了一件奇怪的事情-当键盘显示时,按钮上的文本向上移动,蓝色背景停留在原处:
而且按钮的可见部分根本不可单击。
这是我执行中的某种错误还是问题?
我的代码对此非常简单:
@IBOutlet weak var continueUsernameBottomConstraint: NSLayoutConstraint!
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
NotificationCenter.default.addObserver(self, selector: #selector(tutorialKeyboardWillAppear), name: .UIKeyboardWillShow, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(tutorialKeyboardWillDisappear), name: .UIKeyboardWillHide, object: nil)
}
func tutorialKeyboardWillAppear(notification: NSNotification){
print("KEYBOARD APPEARS")
let endFrame = ((notification as NSNotification).userInfo![UIKeyboardFrameEndUserInfoKey] as! NSValue).cgRectValue
continueUsernameBottomConstraint.constant = view.bounds.height - endFrame.origin.y
self.view.layoutIfNeeded()
}
func tutorialKeyboardWillDisappear(notification: NSNotification){
print("KEYBOARD DISAPPEARS")
let endFrame = ((notification as NSNotification).userInfo![UIKeyboardFrameEndUserInfoKey] as! NSValue).cgRectValue
continueUsernameBottomConstraint.constant = view.bounds.height - endFrame.origin.y
self.view.layoutIfNeeded()
}
最佳答案
用这个
func tutorialKeyboardWillAppear(notification: NSNotification){
print("KEYBOARD APPEARS")
let endFrame = ((notification as NSNotification).userInfo![UIKeyboardFrameEndUserInfoKey] as! NSValue).cgRectValue
continueUsernameBottomConstraint.constant = continueUsernameBottomConstraint.constant + CGFloat(endFrame.height)
self.view.layoutIfNeeded()
}
func tutorialKeyboardWillDisappear(notification: NSNotification){
print("KEYBOARD DISAPPEARS")
let endFrame = ((notification as NSNotification).userInfo![UIKeyboardFrameEndUserInfoKey] as! NSValue).cgRectValue
continueUsernameBottomConstraint.constant = continueUsernameBottomConstraint.constant - CGFloat(endFrame.height)
self.view.layoutIfNeeded()
}
关于ios - 当我的应用程序中出现键盘时,按钮会部分向上移动(仅按钮的文本移动),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/40830410/