UIButton - 封装

UIButton 是开发中高频使用的控件,一个好的封装能大大提高代码的整洁度

1. Target封装

......

2. 协议封装
  • 创建点击协议
@objc protocol BookHeaderViewDelegate: NSObjectProtocol {
    
    optional func headerViewDidClick(index: Int)
}
  • 封装Button
private func buildButtonWithCustom(title: String, frame: CGRect, action: Selector!, tag: NSInteger) {
        let button = UIButton(type: .Custom)
        button.layer.cornerRadius = 3
        button.layer.masksToBounds = true
        button.setTitle(title, forState: .Normal)
        button.setTitleColor(UIColor.whiteColor(), forState: .Normal)
        button.tag = tag
        button.addTarget(self, action: action, forControlEvents: .TouchUpInside)
        button.frame = frame
        button.backgroundColor = UIColor.colorWithCustom(246, g: 36, b: 8)
        addSubview(button)
    }
  • 添加点击方法
private func click(sender: UIButton) {
        delegate?.headerViewDidClick!(sender.tag)
    }
  • 创建按钮
buildButtonWithCustom("菜  谱", frame: CGRectMake(10, 10, (kScreenWith - 30) / 2.0, (kScreenWith - 30)  * 0.618 / 2.0), action: #selector(BookHeaderView.click),tag: 1)
3. 闭包封装

......

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

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 173,498评论 25 708
  • 前言 过年期间,少不了各种聚会,当下聚会大多数情况下自然是团购,然后就是用各种APP。。。使用度娘糯米时(不是广告...
    霖溦阅读 11,885评论 51 162
  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 12,251评论 4 61
  • 学习linux也是在学一门语言,把基础命令比作单词、脚本称之为句子,当你可以把多行脚本组合实现某个功能时你就算会写...
    寻叶亭阅读 120评论 0 0
  • 《冤家父子》有一段父子吵架: 爸爸:“你怎么就不明白我这是为了你好呀!” 儿子:“你怎么就不明白我根本不需要你对我...
    黑白之间阅读 408评论 5 4