隐藏在键盘后面的 UITextfield

UITextfield hidden behind keyboard

我正在构建一个包含搜索或 UITextField 的应用程序,它位于安全区域附近的视图底部,因此当我触摸它并且弹出键盘时,文本字段隐藏在键盘后面.

当键盘弹出时,我怎样才能使文本字段移动到它的正上方? 这是我在 viewController.swift

中的代码

class ViewController: UIViewController {

    @IBOutlet weak var windLabel: UILabel!
    @IBOutlet weak var cityLabel: UILabel!
    @IBOutlet weak var tempLabel: UILabel!
    @IBOutlet weak var searchCItyTextField: UITextField!
    override func viewDidLoad() {
        super.viewDidLoad()
        searchCItyTextField.delegate = self
    }
}
//MARK: - UITextFieldDelegate
extension UIViewController: UITextFieldDelegate {
    public func textFieldShouldReturn(_ textField: UITextField) -> Bool {
        self.view.endEditing(true)
        return true
    }
}

截图如下:

打开 KeyBoard 时使用 IQKeyboardManager 滚动 UITextField

例如

在 AppDelegate 中

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
    // Override point for customization after application launch.
    
    IQKeyboardManager.shared.enable = true 
    return true
  }

在ViewController

class ViewController: UIViewController {
  
  lazy var textField: UITextField! = {
    
    let textField = UITextField()
    textField.translatesAutoresizingMaskIntoConstraints = false
    textField.borderStyle = .roundedRect
    textField.placeholder = "Enter"
    return textField
    
  }()
    
  override var preferredStatusBarStyle: UIStatusBarStyle{
    return .darkContent
  }
  
  override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view.
    self.view.backgroundColor = .white
    self.setConstraint()
    
  }
    
  func setConstraint(){
    
    self.view.addSubview(self.textField)

    NSLayoutConstraint.activate([
    
      self.textField.heightAnchor.constraint(equalToConstant: 60),
      self.textField.widthAnchor.constraint(equalToConstant: UIScreen.main.bounds.width * 0.66),
      self.textField.centerXAnchor.constraint(equalTo: self.view.centerXAnchor),
      self.textField.bottomAnchor.constraint(equalTo: self.view.safeAreaLayoutGuide.bottomAnchor, constant: -10),
      
    ])

  }
  
}

输出:

您可以为文本字段的底部约束添加一个 @IBOutlet 并在键盘 appears/disappears 时更新它。例如,当没有键盘时,将其从底部设置为 -30,如果存在键盘,则将其更新为 -keyboardHeight - 30。请参阅 .

的示例

另一种方法是将整个视图向上移动,例如使用 UIScrollView,当键盘存在并向上滚动时增加其高度。

您可以观察键盘出现或消失,并更新您的界面

例如:

@objc func kbDidShow(notification: Notification) {


        guard let userInfo = notification.userInfo else { return }

        let kbFrameSize = (userInfo[UIResponder.keyboardFrameEndUserInfoKey] as! NSValue).cgRectValue

        yourTextField.bottomAnchor.constraint(equalsTo: self.view.bottomAnchor, constant: kbFrameSize.height + 50).isActive = true
        
    }

@objc func kbDidHide() {

       yourTextField.bottomAnchor.constraint(equalsTo: self.view.bottomAnchor, constant: 50).isActive = true

    }
override func viewDidLoad() {
        super.viewDidLoad()

        NotificationCenter.default.addObserver(self, selector: #selector(kbDidShow), name: UIResponder.keyboardDidShowNotification, object: nil)
        NotificationCenter.default.addObserver(self, selector: #selector(kbDidHide), name: UIResponder.keyboardDidHideNotification, object: nil)
        
        
    }