我知道關于這個主題有幾個問題,但我無法在 [SWIFT 5.0] 或更高版本上找到任何問題,非常感謝您的幫助。
我在螢屏底部有一個 UITextFeild,每次用戶點擊它時都會隱藏它。有沒有最近的方法可以解決這個隱藏問題,方法是 textFeild 跟隨鍵盤到頂部或出現在鍵盤頂部的示例欄位。任何幫助將不勝感激,謝謝!
uj5u.com熱心網友回復:
您可以使用以下代碼:
首先,在控制器的 viewDidLoad() 方法中添加這兩行代碼:
NotificationCenter.default.addObserver(self, selector: #selector(self.keyboardWillShow), name: UIResponder.keyboardWillShowNotification, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(self.keyboardWillHide), name: UIResponder.keyboardWillHideNotification, object: nil)
其次,在控制器中添加這兩個方法:
@objc func keyboardWillShow(notification: NSNotification) {
if yourTextfield.isEditing == true
{
if let keyboardSize = (notification.userInfo?[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue {
self.view.frame.origin.y -= keyboardSize.height
}
}
}
@objc func keyboardWillHide(notification: NSNotification) {
if self.view.frame.origin.y != 0 {
self.view.frame.origin.y = 64
}
}
享受 :)。
uj5u.com熱心網友回復:
如果您使用 AutoLayout,您可以嘗試Keyboard從我收集的名為![如何使用鍵盤移動 UITextFeild 以避免阻塞 [SWIFT 5 ]](https://img.uj5u.com/2022/03/23/a3cc3535c50a41cd921388e5080fdf51.gif)
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/446953.html
標籤:IOS 迅速 键盘 uitextfield
