美文网首页
swift 自定义键盘(UITextField的inputVie

swift 自定义键盘(UITextField的inputVie

作者: 秋叶红90 | 来源:发表于2023-03-21 22:52 被阅读0次

    1 自定义键盘的高度自定义有两种

    一种是用约束,约束无法自定义高度,系统会给默认高度的

    self.testView.translatesAutoresizingMaskIntoConstraints = false
            
            NSLayoutConstraint.activate([NSLayoutConstraint.init(item: self.testView, attribute: NSLayoutConstraint.Attribute.height, relatedBy: NSLayoutConstraint.Relation.equal, toItem: self.testView, attribute: NSLayoutConstraint.Attribute.height, multiplier: 1, constant: cacheCG?.height ?? 200)])
            
            self.textField.inputView = self.testView
    

    一种是用frame 布局, 可以自定义高度完全自己掌控

    //
    //  ViewController.swift
    //  TestDemoKey
    //
    //  Created by Mac on 2023/3/22.
    //
    
    import UIKit
    
    import SnapKit
    class ViewController: UIViewController {
        @IBOutlet weak var textField: UITextField!
        var testView: UIView = UIView.init()
        var testViewH: UIView = UIView.init()
        var cacheCG: CGRect?
        var oldConst: [NSLayoutConstraint] = []
        override func viewDidLoad() {
            super.viewDidLoad()
            // Do any additional setup after loading the view.
            let tap = UITapGestureRecognizer.init(target: self, action: #selector(self.tapClick))
            textField.addGestureRecognizer(tap)
            NotificationCenter.default.addObserver(self, selector: #selector(self.willShow(notifi:)), name: UIResponder.keyboardWillShowNotification, object: nil)
            
            NotificationCenter.default.addObserver(self, selector: #selector(self.willHide(notifi:)), name: UIResponder.keyboardWillHideNotification, object: nil)
            let testView1: UIView = .init()
            testView1.backgroundColor = .blue
            self.testView.addSubview(testView1)
            testView1.snp.makeConstraints { make in
                make.left.top.right.equalTo(0)
                make.bottom.equalTo(-30)
            }
            self.oldConst = self.testView.constraints
        }
    
        @objc  func willHide(notifi: Notification) {
            
           
         }
        @objc  func willShow(notifi: Notification) {
            if self.textField.inputView == nil {
                let value = notifi.userInfo?["UIKeyboardBoundsUserInfoKey"] as? NSValue
                cacheCG = value?.cgRectValue
                print("notifi ===1 \(value?.cgRectValue)")
            } else {
                let value = notifi.userInfo?["UIKeyboardBoundsUserInfoKey"] as? NSValue
                print("notifi ===2 \(value?.cgRectValue)")
            }
            
         }
        
       @objc  func tapClick() {
           self.textField.inputView = nil
           self.textField.inputAccessoryView = nil
           self.textField.reloadInputViews()
           self.textField.becomeFirstResponder()
        }
        @IBAction func texBtnClick(_ sender: Any) {
            if self.textField.inputView != nil {
                self.tapClick()
                return
            }
            testView.backgroundColor = .red
            
            
            
            
            print("notifi === frame === \(cacheCG?.height ?? 200 + 38) === \(self.testView.constraints)")
    //        为了每次都可以自定义高度请这样写
            self.testView.bounds = cacheCG ?? CGRect.init(x: 0, y: 0, width: UIScreen.main.bounds.size.width, height: cacheCG?.height ?? 200)
    //        self.testView.removeConstraints(self.testView.constraints)
            self.testView.translatesAutoresizingMaskIntoConstraints = true
    //        self.testView.constraints.forEach { item in
    //            print("item == \(item.firstItem) \(item.secondItem) == \(item.firstAttribute == .height)  \(item.secondAttribute == .height)")
    //            if  item.firstAttribute == .height || item.secondAttribute == .height {
    //                self.testView.removeConstraint(item)
    //            }
    //        }
            self.testView.removeConstraints(self.testView.constraints)
            self.testView.addConstraints(self.oldConst)
            self.textField.inputView = self.testView
            
            
            self.textField.reloadInputViews()
            self.textField.becomeFirstResponder()
        }
        
    }
    
    
    

    相关文章

      网友评论

          本文标题:swift 自定义键盘(UITextField的inputVie

          本文链接:https://www.haomeiwen.com/subject/axhvrdtx.html