初始化后在UITextField的子类上设置rightview

时间:2016-09-29 08:14:38

标签: ios swift uitextfield

我尝试在UITextField初始化后设置rightView,但它得到nil。这是我的班级:

class MyTextField: UITextField, UIGestureRecognizerDelegate {  
    public required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        prepare()
    }

    public override init(frame: CGRect) {
        super.init(frame: frame)
        prepare()
    }

    public convenience init() {
        self.init(frame: .zero)
    }

    open private(set) var errorUIImageView = UIImageView(image: (resourceName: "ic_error_red"))

    open func prepare() {
        print("prepare")
        let tap = UITapGestureRecognizer(target: self, action: Selector(("handleTap:")))
        tap.numberOfTapsRequired = 1
        tap.delegate = self
        errorUIImageView.isUserInteractionEnabled = true
        errorUIImageView.addGestureRecognizer(tap)
        self.rightView = errorUIImageView // setting the rightView here
    }

    @IBInspectable
    open var detail: String? {
        get {
            return detailLabel.text
        }
        set(value) {
            detailLabel.text = value
            if let v: String = value {
                self.rightViewMode = .always

                if self.rightView == nil {
                    print("nil") // this line is reached!
                }
            }
            else {
                self.rightViewMode = .never;
            }
        }
    }

    @IBInspectable
    open var detailColor = UIColor.red {
        didSet {
            if let v: String = detailLabel.text {
                detailLabel.attributedText = NSAttributedString(string: v, attributes: [NSForegroundColorAttributeName: detailColor])
            }
        }
    }

    @IBInspectable
    open private(set) lazy var detailLabel = UILabel(frame: .zero)

    func handleTap(sender: UITapGestureRecognizer) {

        print("tapped")
    }
}

大多数代码都来自Material

1 个答案:

答案 0 :(得分:0)

如果您edited首次在interface builder中显示实施后=(SUM(Fields!FirstCallResolution.Value) / COUNT(Fields!CommunicationId.Value)) ,则可能需要刷新所有视图。

enter image description here