0
我正在開發一個項目,我正在開發一個具有兩個文本框的UIView的登錄屏幕,並在鍵盤隱藏或顯示時更改該視圖的位置。iOS Swift UiView框架以奇怪的方式變化
我從電子郵件文本字段移至密碼文本字段時出現問題。該視圖返回到其原始位置/框架而不要求它。
這裏的代碼片段:
var isKeyboardOpened = false
@IBOutlet weak var viewLoginContainer: UIView!
override func viewDidLoad() {
super.viewDidLoad()
NotificationCenter.default.addObserver(self, selector: #selector(self.keyboardWillShow), name: NSNotification.Name.UIKeyboardWillShow, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(self.keyboardWillHide), name: NSNotification.Name.UIKeyboardWillHide, object: nil)
}
func keyboardWillShow(notification: NSNotification) {
if let keyboardSize = (notification.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue {
if !isKeyboardOpened {
isKeyboardOpened = true
self.viewLoginContainer.frame.origin.y -= keyboardSize.height - 100
self.viewLoginContainer.layoutIfNeeded()
}
}
}
func keyboardWillHide(notification: NSNotification) {
if let keyboardSize = (notification.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue {
if self.view.frame.origin.y != 0{
isKeyboardOpened = false
self.viewLoginContainer.frame.origin.y += keyboardSize.height - 100
}
}
}
您可以設置佈局約束編程與自動佈局,堅持你想要的佈局。 –
你爲什麼要檢查'self.view.frame.origin.y'? –
這是一個在網絡上的解決方案,我很沮喪,但我刪除它,它是一樣的 – Sob7y