Swift给UITextField添加右边按钮

不哔哔,上代码:

extension UITextField {
    func setupShowPasswordButton() {
        let eyesButton = UIButton(type: .System)
        eyesButton.frame = CGRect(x: 0, y: 0, w: 16, h: 16)
        eyesButton.setImage(UIImage(named: "imgEyes"), forState: .Normal)
        eyesButton.tintColor = UIColor.blue4Color
        eyesButton.rx_tap.subscribeNext { [unowned self] in
            self.secureTextEntry = !self.secureTextEntry
            eyesButton.tintColor = self.secureTextEntry ? UIColor.blue4Color : UIColor.blue6Color
            if self.isFirstResponder() { self.becomeFirstResponder() }
            
            }.addDisposableTo(disposeBag)
        rightView = eyesButton
        rightViewMode = .Always
    }
    
    func setupClearButton() {
        let clearButton = UIButton(type: .System)
        clearButton.frame = CGRect(x: 0, y: 0, w: 16, h: 16)
        clearButton.setImage(UIImage.imageWithIconFont(.CloseHL, iconColor: UIColor.blue4Color, fontSize: 24), forState: .Normal)
        clearButton.rx_tap.subscribeNext { [unowned self] in
            self.text = nil
            
            }.addDisposableTo(disposeBag)
        rightView = clearButton
        rightViewMode = .WhileEditing
    }
}

你可能感兴趣的:(Swift给UITextField添加右边按钮)