iOS UIViewController 键盘问题处理

423 阅读1分钟

1. 把所有键盘相关的操作函数加到协议里

    /// 不用担心被键盘挡住的最大高度
   func safeMaxHeight() -> CGFloat
    /// 添加对键盘的活动的监控
   func addNoticeForKeyboard()
    //deinit 调用
   func removeNotice()
    //键盘出现调用
   func updateSubviewFrame(notification: NSNotification)
    //键盘消失调用
   func recoverSubViewFrame(notification: NSNotification)
    
    /// 让键盘消失
    /// - Parameter sender: active input item
    func hiddenKeyboard(sender: UIView) 

2. 让UIViewController 遵循协议,处理关于键盘的各种事项

import UIKit

class ViewController: UIViewController, KeyboardUpDownProtocol {
    var textV: UITextView!   //input item
    let textVMinY: CGFloat = 500  //orignal
    override func viewDidLoad() {
        super.viewDidLoad()
        textV = UITextView(frame: CGRect(x: (UIScreen.main.bounds.width - 300)/2, y: textVMinY, width: 300, height: 150))
        textV.layer.borderWidth = 1.0
        textV.layer.borderColor = UIColor.gray.cgColor
        view.addSubview(textV)
        addNoticeForKeyboard()
        let tap = UITapGestureRecognizer(target: self, action: #selector(tapAction(tap:)))
        self.view.addGestureRecognizer(tap)
    }
    
    deinit {
        removeNotice()
    }
    
    @objc func tapAction(tap :UITapGestureRecognizer) {
        hiddenKeyboard(sender: textV)
    }
    
    func safeMaxHeight() -> CGFloat {
        return textV.frame.origin.y + textV.frame.size.height
    }
    
    func addNoticeForKeyboard() {
        NotificationCenter.default.addObserver(self,
                                               selector: #selector(updateSubviewFrame(notification:)),
                                               name: UIResponder.keyboardWillShowNotification,
                                               object: nil)
        NotificationCenter.default.addObserver(self,
                                               selector: #selector(recoverSubViewFrame(notification:)),
                                               name: UIResponder.keyboardWillHideNotification,
                                               object: nil)
    }
    //deinit 调用
    func removeNotice() {
        NotificationCenter.default.removeObserver(self)
    }
    //键盘出现调用
    @objc func updateSubviewFrame(notification: NSNotification)  {
        let coincide = safeMaxHeight() - (self.view.frame.size.height - UI.keyboardHeight - UI.beautifulSpace)
        if coincide >= 0 { //need adjust
            self.textV.frame = CGRect(x: textV.frame.origin.x, y: textVMinY - coincide, width: textV.frame.width, height: textV.frame.height)
        }else {
            self.textV.frame = CGRect(x: textV.frame.origin.x, y: textVMinY, width: textV.frame.width, height: textV.frame.height)
        }
    }
    //键盘消失调用
    @objc func recoverSubViewFrame(notification: NSNotification)  {
        if self.textV.frame.origin.y != textVMinY {
            self.textV.frame = CGRect(x: textV.frame.origin.x, y: textVMinY, width: textV.frame.width, height: textV.frame.height)
        }
    }
    
    func hiddenKeyboard(sender: UIView) {
        sender.resignFirstResponder()
    }
}

struct UI {
   static let beautifulSpace: CGFloat = 13.0
   static var keyboardHeight: CGFloat {
        switch UIScreen.main.bounds.size.height {
        case 885 ... 1000 :
                return 346
        case 811 ..< 885 :
            return 336
        case 735 ..< 811:
            return 271
        case 666 ..< 735:
            return 260
        case 567 ..< 666:
            return 253
        default:
            return 253
        }
    }
}

demo 地址: github.com/gree180160/…