How to solve keyboard problems in Swift 3?

*爱你&永不变心* 提交于 2019-12-20 06:30:22

问题


The problem is that when I try to write in the text fields the keyboard cover them up. How can I scroll the text field up to see what am I writing. I have below lines of code to enable the return key and to hide the keyboard when you touch in a different place:

override func didReceiveMemoryWarning() {
  super.didReceiveMemoryWarning()
  // Dispose of any resources that can be recreated.

}

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
    self.view.endEditing(true)
}

func textFieldShouldReturn(_ textField: UITextField) -> Bool {
    self.view.endEditing(true)
    return false
}

回答1:


How can I scroll the text field up to see what am I writing

This can be achieved in two ways:

  1. You can manage your frame programmatically on keyboard hide and show like @Taichi Kato
  2. You can integrate libraries which serves the same purpose. One such library is IQKeyBoardManager with its Swift variant as IQKeyboardManagerSwift You can find it on GitHub and cocoapods

To achieve follow the below steps :

SWIFT 3

  1. Just install IQKeyboardManagerSwift from Github or cocoapods

  2. Import IQKeyboardManagerSwift in Appdelegate

  3. Add the below lines of code in AppDelegate didFinishLaunchingWithOptions method.

    IQKeyboardManager.sharedManager().shouldResignOnTouchOutside = true;
    

Objective-C

  1. Install IQKeyBoardManager via any medium
  2. Import #import "IQKeyboardManager.h" in Appdelegate
  3. Add the below lines of code in AppDelegate didFinishLaunchingWithOptions method.

    IQKeyboardManager.sharedManager.shouldResignOnTouchOutside = true;
    

And this is Done. This is the only code which you need to write.




回答2:


The easiest solution to this problem is to put all the elements into one scrollview and then add the keyboard height to the constant of the bottom of the view to superview.

When the keyboard is shown or hidden, iOS sends out the following notifications to any registered observers:

UIKeyboardWillShowNotification UIKeyboardDidShowNotification UIKeyboardWillHideNotification UIKeyboardDidHideNotification

So here is what you can do:

  1. Get the size of the keyboard.
  2. Adjust the bottom content inset of your scroll view by the keyboard height.
  3. Scroll the target text field into view.

Something like this:

 func keyboardWillShow(notification: NSNotification) {
        print("KEYBOARD WILL SHOW")
        let userInfo:NSDictionary = notification.userInfo! as NSDictionary
        let keyboardFrame:NSValue = userInfo.value(forKey: UIKeyboardFrameEndUserInfoKey) as! NSValue
        let keyboardRectangle = keyboardFrame.cgRectValue
        let keyboardHeight = keyboardRectangle.height
        bottomConstraint.constant = keyboardHeight + 8
        UIView.animate(withDuration: 0.5, animations: { [weak self] in
            self?.view.layoutIfNeeded() ?? ()
        })

        UIView.animate(withDuration: 0.3) {
            self.view.layoutIfNeeded()
        }
    }
    func dismissKeyboard() {
        //Causes the view (or one of its embedded text fields) to resign the first responder status.
        view.endEditing(true)
        bottomConstraint.constant = 8
        UIView.animate(withDuration: 0.5, animations: { [weak self] in
            self?.view.layoutIfNeeded() ?? ()
        })
    }


来源:https://stackoverflow.com/questions/40839366/how-to-solve-keyboard-problems-in-swift-3

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!