您的位置:首页 > 移动开发 > Swift

IOS开发系列之Swift_UI_Btn

2016-03-15 11:50 357 查看
原文链接:https://www.geek-share.com/detail/2669013803.html

import UIKit

 

class ViewController: UIViewController {

 

    //声明一个btn

    var exampleBtn : UIButton!

    

    override func viewDidLoad() {

        super.viewDidLoad()

        //声明一个方法

        makeBtn()

        // Do any additional setup after loading the view, typically from a nib.

    }

 

    //方法

    private func makeBtn() {

        //初始化

        self.exampleBtn = UIButton.init()

        //设置frame

        self.exampleBtn.frame = CGRectMake(100, 100, 60, 60)

        //设置背景颜色

        //self.exampleBtn.backgroundColor = UIColor.lightGrayColor()

        //设置btn 字体大小

        self.exampleBtn.titleLabel?.font = UIFont.systemFontOfSize(12)

        //设置btn 的文字

        self.exampleBtn.setTitle("clickBnt", forState: UIControlState.Normal)

        //标记tag

        self.exampleBtn.tag = 520

        //添加点击事件

        self.exampleBtn.addTarget(self, action: "clickBtnDown:", forControlEvents: UIControlEvents.TouchUpInside)

        //设置btn的图片

        self.exampleBtn.setImage(UIImage(imageLiteral: "103"), forState: UIControlState.Normal)

        self.view .addSubview(self.exampleBtn)

    }

    

    //btn 的点击事件

    func clickBtnDown(btn : UIButton) {

        print("\(btn.tag)")

    }

    

    

    

    override func didReceiveMemoryWarning() {

        super.didReceiveMemoryWarning()

        // Dispose of any resources that can be recreated.

    }

 

 

}

转载于:https://www.cnblogs.com/godlovexq/p/5278790.html

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: