swift Swift - 当UITextField被键盘隐藏时滚动查看

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了swift Swift - 当UITextField被键盘隐藏时滚动查看相关的知识,希望对你有一定的参考价值。

01. Write your UIViewController class like this:

import UIKit

class UserProfileViewController: UIViewController {
 
    override func viewDidLoad() {
        super.viewDidLoad()
        self.hideKeyboardWhenTappedAround()
        addKeyboardObservers()
    }
    
    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)
        removeKeyboardObservers()

    }
}

02. Create a class extension like this:




import UIKit

extension UIViewController {

    
    func hideKeyboardWhenTappedAround() {
        let tap: UITapGestureRecognizer =     UITapGestureRecognizer(target: self, action:    #selector(UIViewController.dismissKeyboard))
        tap.cancelsTouchesInView = false
        view.addGestureRecognizer(tap)
        print ("hideKeyboardWhenTapped")
    }
    @objc func dismissKeyboard() {
        view.endEditing(true)
        print ("dismissKeyboard")
    }
    
    func addKeyboardObservers() {
        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)
        print ("addKeyboardObservers")
    }
    
    func removeKeyboardObservers() {
        NotificationCenter.default.removeObserver(self, name: NSNotification.Name.UIKeyboardWillShow, object: self.view.window)
        NotificationCenter.default.removeObserver(self, name: NSNotification.Name.UIKeyboardWillHide, object: self.view.window)
        print ("removeKeyboardObservers")
    }
    
    @objc func keyboardWillShow(notification: NSNotification) {
        if let userInfo = notification.userInfo {
            if let keyboardSize = (userInfo[UIKeyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue {
                print("Keyboard Size: \(keyboardSize)")
                let keyboardHeight = keyboardSize.size.height
                if let activeTextField = UIResponder.currentFirst() as? UITextField {
                    if keyboardSize.contains(activeTextField.frame.origin) {
                        UIView.animate(withDuration: 0.1, animations: { () -> Void in
                            self.view.window?.frame.origin.y = -1 * keyboardHeight
                            self.view.layoutIfNeeded()
                            print("keyboardWillShow \(String(describing: self.view.window?.frame.origin.y))")
                        })
                    }
                }
            }
        }

    }
    
    @objc func keyboardWillHide(notification: NSNotification) {
        UIView.animate(withDuration: 0.1, animations: { () -> Void in
            self.view.window?.frame.origin.y = 0
            self.view.layoutIfNeeded()
            self.view.setNeedsUpdateConstraints()
            print("keyboardWillHide \(String(describing: self.view.window?.frame.origin.y))")
        })
    }

    
    func textFieldDidBeginEditing(textField: UITextField) {
        print ("textFieldDidBeginEditing")
    }
    
    func textFieldDidEndEditing(textField: UITextField) {
        print ("textFieldDidEndEditing")
    }
    
    func resignTextFieldFirstResponders() {
        for textField in self.view.subviews where textField is UITextField {
            textField.resignFirstResponder()
            print ("resignTextFieldFirstResponders")
        }
    }
    
    
}

//To identify the textField with focus, you need to search for the object that has become a first responder. First responder object is the one one having focus for user input. 
public extension UIResponder {
    
    private struct Static {
        static weak var responder: UIResponder?
    }
    
    public static func currentFirst() -> UIResponder? {
        Static.responder = nil
        UIApplication.shared.sendAction(#selector(UIResponder._trap), to: nil, from: nil, for: nil)
        return Static.responder
    }
    
    @objc private func _trap() {
        Static.responder = self
    }
}

以上是关于swift Swift - 当UITextField被键盘隐藏时滚动查看的主要内容,如果未能解决你的问题,请参考以下文章

swift Swift - 当UITextField被键盘隐藏时滚动查看

Swift:当键盘显示时向上滚动视图

Swift 3 - 当布尔为真时如何移动精灵节点?

不...当 UIButton (Swift) 中的文本太长时

Swift:仅当键盘隐藏 TextField 或 Button 时滚动视图

当 Struct 的特定属性发生突变时通知父级,Swift