IOS开发系列之Swift

mac2022-06-30  8

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

相关资源:SwiftUI Button自定义风格
最新回复(0)