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()
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 216,142评论 6 498
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 92,298评论 3 392
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 162,068评论 0 351
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,081评论 1 291
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,099评论 6 388
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,071评论 1 295
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 39,990评论 3 417
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 38,832评论 0 273
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,274评论 1 310
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,488评论 2 331
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,649评论 1 347
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,378评论 5 343
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 40,979评论 3 325
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,625评论 0 21
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,796评论 1 268
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 47,643评论 2 368
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,545评论 2 352

推荐阅读更多精彩内容