美文网首页
Swift给UITextField添加右边按钮

Swift给UITextField添加右边按钮

作者: 飛呈Geek | 来源:发表于2016-08-12 15:22 被阅读414次

不哔哔,上代码:

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添加右边按钮

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