IQKeyboardManager失效导致输入框被键盘遮挡的问题

弹出的View加在了window上,导致不能解决键盘遮挡的问题。

弹出view的代码:

//
//  TipEditView.swift
//  HaidilaoPad
//
//  Created by 彭思 on 2018/12/6.
//  Copyright © 2018年 HongHuoTai. All rights reserved.
// 小费编辑View

import UIKit

enum TipType: String {
    case cashTipType = "1"
    case cardTipType = "2"
}

protocol TipEditViewDelegate: NSObjectProtocol {
    func tipEditSureBtnDidClick(model: BillListModel, tipMoney: String, tipType: TipType, remark: String)
}

class TipEditView: UIView {

    var delegate: TipEditViewDelegate?
    var settedTipMoney: String?
    var model: BillListModel?
    var vc = UIViewController()
    var fromVC = UIViewController()
    
    struct Action {
        static let cancleAction = #selector(TipEditView.cancleAction)
        static let sureAction = #selector(TipEditView.sureAction)
    }
    
    private lazy var bgView: UIView = {
        let view = UIView()
        view.backgroundColor = UIColor.white
        view.layer.cornerRadius = 4.0
        view.layer.masksToBounds = true
        view.isUserInteractionEnabled = true
        return view
    }()
    
    private lazy var tipMoneyLabel: UILabel = {
        let label = UILabel()
        label.textAlignment = .left
        label.text = GLOBAL_LANGUAGE("小费金额") + ":"
        label.font = UIFont.systemFont(ofSize: 18)
        label.adjustsFontSizeToFitWidth = true
        return label
    }()
    
    private lazy var underLine: UILabel = {
        let label = UILabel()
        label.backgroundColor = UIColor.gray
        label.font = UIFont.systemFont(ofSize: 18)
        label.textColor = UIColor.customLineColor
        return label
    }()
    
    fileprivate lazy var tipMoneyTextField: UITextField = {
        let textField = UITextField()
        textField.delegate = self
        textField.backgroundColor = UIColor.clear
        textField.font = UIFont.systemFont(ofSize: 18)
        textField.textAlignment = .center
        return textField
    }()
    
    private lazy var remarkLabel: UILabel = {
        let label = UILabel()
        label.textAlignment = .left
        label.text = GLOBAL_LANGUAGE("备注") + ":"
        label.font = UIFont.systemFont(ofSize: 18)
        label.adjustsFontSizeToFitWidth = true
        return label
    }()
    
    fileprivate lazy var cashBtn: UIButton = {
        let btn = UIButton(type: .custom)
        btn.isSelected = false
        btn.setImage(UIImage(named: "circleStateBtnNormal"), for: .normal)
        btn.setImage(UIImage(named: "circleStateBtnSelected"), for: .selected)
        btn.setImage(UIImage(named: "circleStateBtnSelected"), for: .highlighted)
        btn.titleLabel?.adjustsFontSizeToFitWidth = true
        btn.setTitle(GLOBAL_LANGUAGE("现金小费"), for: .normal)
        btn.setTitleColor(UIColor.black, for: .normal)
        btn.addTarget(self, action: #selector(cashTipAction(_:)), for: .touchUpInside)
        btn.titleLabel?.adjustsFontSizeToFitWidth = true
        return btn
    }()
    
    fileprivate lazy var cardBtn: UIButton = {
        let btn = UIButton(type: .custom)
        btn.isSelected = false
        btn.setImage(UIImage(named: "circleStateBtnNormal"), for: .normal)
        btn.setImage(UIImage(named: "circleStateBtnSelected"), for: .selected)
        btn.setImage(UIImage(named: "circleStateBtnSelected"), for: .highlighted)
        btn.titleLabel?.adjustsFontSizeToFitWidth = true
        btn.setTitle(GLOBAL_LANGUAGE("刷卡小费"), for: .normal)
        btn.setTitleColor(UIColor.black, for: .normal)
        btn.addTarget(self, action: #selector(cardTipAction(_:)), for: .touchUpInside)
        btn.titleLabel?.adjustsFontSizeToFitWidth = true
        return btn
    }()
    
    private lazy var textView: PlaceholderTextView = {
        let textView = PlaceholderTextView()
        textView.font = UIFont.systemFont(ofSize: 18)
        textView.placeholder = GLOBAL_LANGUAGE("请输入备注内容")
        textView.backgroundColor = UIColor(hexString: "f9f9f9")
        textView.layer.borderColor = UIColor(hexString: "aeaeae").cgColor
        textView.layer.borderWidth = 0.5
        textView.layer.cornerRadius = 2
        textView.delegate = self
        return textView
    }()
    
    private lazy var sureBtn: UIButton = {
        let btn = UIButton()
        btn.setTitleColor(UIColor(hexString: "FFFFFF"), for: .normal)
        btn.setTitle(GLOBAL_LANGUAGE("确定"), for: .normal)
        btn.titleLabel?.font = UIFont.systemFont(ofSize: 18)
        btn.backgroundColor = UIColor.unityRedColor()
        btn.addTarget(self, action: Action.sureAction, for: .touchUpInside)
        btn.layer.cornerRadius = 4.0
        btn.layer.masksToBounds = true
        btn.titleLabel?.adjustsFontSizeToFitWidth = true
        return btn
    }()
    
    private lazy var cancleBtn: UIButton = {
        let btn = UIButton()
        btn.setTitleColor(UIColor.black, for: .normal)
        btn.setTitle(GLOBAL_LANGUAGE("取消"), for: .normal)
        btn.titleLabel?.font = UIFont.systemFont(ofSize: 18)
        btn.backgroundColor = UIColor.white
        btn.layer.borderColor = UIColor.black.cgColor
        btn.layer.borderWidth = 0.5
        btn.addTarget(self, action: Action.cancleAction, for: .touchUpInside)
        btn.layer.cornerRadius = 4.0
        btn.layer.masksToBounds = true
        btn.titleLabel?.adjustsFontSizeToFitWidth = true
        return btn
    }()
    
    init(fromVC: UIViewController) {
        let window = UIApplication.shared.windows[0]
        super.init(frame: window.bounds)
        self.fromVC = fromVC
        configureUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func configureUI() {
        bgView.clipsToBounds = true
        bgView.layer.cornerRadius = 4
        self.backgroundColor = UIColor.init(red: 0, green: 0, blue: 0, alpha: 0.7)
        cashBtn.isSelected = true
        self.addSubview(bgView)
        bgView.addSubview(tipMoneyLabel)
        bgView.addSubview(underLine)
        bgView.addSubview(tipMoneyTextField)
        bgView.addSubview(cashBtn)
        bgView.addSubview(cardBtn)
        bgView.addSubview(remarkLabel)
        bgView.addSubview(textView)
        bgView.addSubview(sureBtn)
        bgView.addSubview(cancleBtn)
        
        bgView.snp.makeConstraints { (make) in
            make.centerX.equalTo(self)
            make.top.equalTo((k_ScreenHeight - 320) / 2)
            make.width.equalTo(450)
            make.height.equalTo(320)
        }
        tipMoneyLabel.snp.makeConstraints { (make) in
            make.left.equalTo(bgView.snp.left).offset(30)
            make.top.equalTo(bgView.snp.top).offset(30)
            make.width.equalTo(100)
        }
        underLine.snp.makeConstraints { (make) in
            make.left.equalTo(tipMoneyLabel.snp.right).offset(10)
            make.bottom.equalTo(tipMoneyLabel)
            make.width.equalTo(120)
            make.height.equalTo(1)
        }
        tipMoneyTextField.snp.makeConstraints { (make) in
            make.left.equalTo(underLine)
            make.right.equalTo(underLine)
            make.bottom.equalTo(underLine.snp.top).inset(2)
        }
        cashBtn.snp.makeConstraints { (make) in
            make.left.equalTo(bgView.snp.left).offset(20)
            make.top.equalTo(underLine.snp.bottom).offset(10)
            make.size.equalTo(CGSize(width: 120, height: 40))
        }
        
        cardBtn.snp.makeConstraints { (make) in
            make.left.equalTo(cashBtn.snp.right).offset(20)
            make.top.equalTo(underLine.snp.bottom).offset(10)
            make.size.equalTo(CGSize(width: 100, height: 40))
        }
        
        remarkLabel.snp.makeConstraints { (make) in
            make.top.equalTo(cashBtn.snp.bottom).offset(10)
            make.width.equalTo(60)
            make.left.equalTo(tipMoneyLabel)
        }
        
        textView.snp.makeConstraints { (make) in
            make.top.equalTo(remarkLabel)
            make.left.equalTo(remarkLabel.snp.right).offset(10)
            make.right.equalTo(bgView).inset(30)
            make.height.equalTo(120)
        }
        
        sureBtn.snp.makeConstraints { (make) in
            make.left.equalTo(bgView).offset(80)
            make.top.equalTo(textView.snp.bottom).offset(20)
            make.width.equalTo(100)
            make.height.equalTo(40)
        }
        
        cancleBtn.snp.makeConstraints { (make) in
            make.right.equalTo(bgView.snp.right).inset(80)
            make.centerY.equalTo(sureBtn)
            make.width.height.equalTo(sureBtn)
        }
    }
    
    func setData(model: BillListModel) {
        self.tipMoneyTextField.text = String.stringValue(model.tipAmount)
        self.textView.text = model.memo
    }
    
    @objc func cashTipAction(_ sender: Any) {
        cardBtn.isSelected = false
        cashBtn.isSelected = true
    }
    
    @objc func cardTipAction(_ sender: Any) {
        cardBtn.isSelected = true
        cashBtn.isSelected = false
    }
    
    func clearData() {
        tipMoneyTextField.text = ""
        textView.text = ""
    }
    
    func show() {
        vc.view = self
        vc.modalPresentationStyle = .custom
        self.fromVC.present(vc, animated: false, completion: nil)
    }
    
    func dismiss() {
        vc.dismiss(animated: false, completion: nil)
    }
    
    @objc func cancleAction() {
        self.clearData()
        self.dismiss()
    }
    
    @objc func sureAction() {
        let remarkStr = textView.text ?? ""
        if (tipMoneyTextField.text?.isEmpty)! {
            HUDManager.showAutoDismissFailedMessage(GLOBAL_LANGUAGE("请输入小费金额"))
            return
        }
        //        if Int.intValue(tipMoneyTextField.text) > 999999999999 {
        //            HUDManager.showAutoDismissFailedMessage(GLOBAL_LANGUAGE("小费金额过大"))
        //            tipMoneyTextField.text = ""
        //            tipMoneyTextField.becomeFirstResponder()
        //            return
        //        }
        if remarkStr.isEmpty {
            HUDManager.showAutoDismissFailedMessage(GLOBAL_LANGUAGE("备注不能为空"))
            return
        }
        if remarkStr.count > 0 {
            if remarkStr.count > 32 {
                HUDManager.showAutoDismissFailedMessage(GLOBAL_LANGUAGE("备注最多只能写32个字哦~"))
                return
            }
            guard remarkStr.isValiteSpecificChars(nameType: "备注") else {
                return
            }
        }
        let tipType: TipType = cardBtn.isSelected ? .cardTipType : .cashTipType
        self.delegate?.tipEditSureBtnDidClick(model:self.model!, tipMoney: tipMoneyTextField.text ?? "", tipType: tipType, remark: textView.text ?? "")
    }
}

extension TipEditView: UITextViewDelegate {
    
    func textViewDidEndEditing(_ textView: UITextView) {
        if  textView.text.count > 32 {
            textView.text = textView.text[0..<32]
            textView.resignFirstResponder()
            HUDManager.showAutoDismissFailedMessage(GLOBAL_LANGUAGE("备注最多只能写32个字哦~"))
        }
    }
}

extension TipEditView: UITextFieldDelegate {
    func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
        guard let text = textField.text else{
            return true
        }
        let textLength = text.count + string.count - range.length
        return textLength <= 12
    }
}

使用:

self?.tipEditView = TipEditView(fromVC: self!)
self?.tipEditView?.model = self!.tableDataSource[indexPath.row]
self?.tipEditView?.setData(model: self!.tableDataSource[indexPath.row])
self?.tipEditView?.delegate = self
self?.tipEditView?.show()

你可能感兴趣的:(IQKeyboardManager失效导致输入框被键盘遮挡的问题)