Swift 2.2 Button简单封装

import UIKit

        typealias tapHandler = (sender:UIButton) -> ()

        class BaseUIButton: UIButton {

            var heandler: tapHandler!
            
            class func button(frame:CGRect,title:String?,fontFloat:CGFloat,image:UIImage?,color:UIColor?,handler:tapHandler)-> (UIButton){
                
                let button = BaseUIButton()
                button.frame = frame
                button.titleLabel?.textAlignment = NSTextAlignment.Center
                button.titleLabel?.font = UIFont.systemFontOfSize(fontFloat)
                button.backgroundColor = color

                button.setTitle(title, forState: UIControlState.Normal)
                button.setImage(image, forState: UIControlState.Normal)
                
                
                button.heandler = handler
                ///点击
                button.addTarget(button, action: #selector(btnClick(_:)), forControlEvents: UIControlEvents.TouchUpInside)
                
                return button
            }
          
           func btnClick(btn:UIButton){
                ///调用block
                heandler(sender: btn)
                
            }
            
        }
  • 看我那么可爱n(≧▽≦)n
  • 关注我的微薄 (梁同桌):http://weibo.com/tongrenyinsheng
  • 网站(同人音声) http://www.tongrenyinsheng.com
  • ios 个人写的app (同人音声)ASMR音乐

你可能感兴趣的:(Swift 2.2 Button简单封装)