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
    }
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 174,130评论 25 709
  • 有没有这样一个人,他霸占了你的整个青春。可以因为他的一个微笑神魂颠倒,也因为他的难过而难过。 哔哔就是这样一个人。...
    沐小英阅读 2,342评论 0 9
  • error code(错误代码)=0是操作成功完成。error code(错误代码)=1是功能错误。error c...
    Heikki_阅读 3,462评论 1 9
  • 图一 十朵姐妹花相聚在紫金 说实在,相比于白天重逢时的热闹,晚上静下心来后,我心里是难过的。 我知道,我是敏感的人...
    大吃货霞阅读 242评论 0 0
  • 姐姐有双神奇的手 这双手会劈柴 会烧火 会做饭 会洗衣 会握着我的手教我写字 姐姐的手很大 很温暖 它会在刺骨的寒...
    夜雨寄苝阅读 524评论 5 20