如何用键盘移动 UITextFeild 以避免阻塞 [SWIFT 5+ ]

发布于 2025-01-16 00:23:16 字数 168 浏览 2 评论 0原文

我知道关于这个主题有几个问题,但我在 [SWIFT 5.0] 或更高版本上找不到任何问题,非常感谢您的帮助。

我在屏幕底部有一个 UITextFeild,每次用户单击它时它都会被隐藏。有没有一种最近的方法可以解决这个隐藏问题,通过键盘后面的文本字段到顶部或出现在键盘顶部的示例字段。任何帮助将不胜感激,谢谢!

I know that there are several questions on this topic, but i have not been able to find any on [SWIFT 5.0] or more, and would really appreciate your help.

I have a UITextFeild at the bottom of the screen which gets hidden every time the user clicks on it. Is there a recent method on which i can solve this hiding issue, by either the textFeild following the keyboard to the top or a sample field appearing on top of the keyboard. Any help would be greatly appreciated, thank you!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

关于从前 2025-01-23 00:23:17

您可以使用以下代码:

首先,在控制器 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
    }
}

Enjoy :)。

You can use the following code:

First, add these two lines of code in your controller viewDidLoad() method:

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)

Second, add these two methods inside your controller:

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

Enjoy :).

看海 2025-01-23 00:23:17

如果您使用 AutoLayout,您可以尝试使用我的名为 SwiftToolkit

将此代码添加到您的控制器 viewDidLoad() 方法中:

let keyboardGuide = KeyboardLayoutGuide()
view.addLayoutGuide(keyboardGuide)

/** 
    Your textField vertical position constraint 
    must have lower priority than this constraint
*/
keyboardGuide.topAnchor.constraint(greaterThanOrEqualTo: textField.bottomAnchor, constant: 8).isActive = true

当键盘出现时,它将推送您的UITextField 像这样:

在此处输入图像描述

If you use AutoLayout you may try Keyboard and KeyboardLayoutGuide from my collection of handy Swift extensions and classes called SwiftToolkit

Add this code to your controller viewDidLoad() method:

let keyboardGuide = KeyboardLayoutGuide()
view.addLayoutGuide(keyboardGuide)

/** 
    Your textField vertical position constraint 
    must have lower priority than this constraint
*/
keyboardGuide.topAnchor.constraint(greaterThanOrEqualTo: textField.bottomAnchor, constant: 8).isActive = true

When the keyboard appears it will push your UITextField up like this:

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文