微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

iOS-Swift-UIButton

//

//  ViewController.swift

//  Label

//

//  Created by 赵士军 on 2019/11/18.

//  copyright © 2019 赵士军. All rights reserved.

//

 

import UIKit

 

class ViewController: UIViewController {

 

    override func viewDidLoad() {

        super.viewDidLoad()

        //按钮

        self .setupBtn()

        // Do any additional setup after loading the view.

    }

    

    func setupBtn() {

        //初始化Btn

        let testBtn = UIButton.init(type: .custom)

        //设置位置大小

        testBtn.frame = CGRect(x: 10, y: 10, width: 200, height: 200)

      //设置标题

        testBtn .setTitle("点击我", for: .normal)

        //设置颜色

        testBtn .setTitleColor(.red, for: .normal)

    // 设置字体大小

        testBtn.titleLabel?.font = .boldSystemFont(ofSize: 18)

        //添加点击事件1

        testBtn .addTarget(self, action: #selector(testButtonClick), for: .touchUpInside)

        //事件2

        testBtn .addTarget(self, action: #selector(buttonClick(_button:)), for: .touchUpInside)

        //标记

        testBtn.tag=18

        //圆角

         testBtn.layer.cornerRadius = 100

        //

        testBtn.clipsToBounds = true

        //中心

        testBtn.center = self.view.center

        

        testBtn.layer.borderWidth = 12

        testBtn.layer.borderColor = UIColor.red.cgColor

        testBtn.backgroundColor = .green

        self.view .addSubview(testBtn)

    

    }

    @objc func testButtonClick(){

        

        print("点击我")

    }

    @objc func buttonClick(_button:UIButton){

        

        print(String.init(format: "点击了%@---%d", _button,_button.tag))

    }

    /*

    // MARK: - Navigation

 

    // In a storyboard-based application, you will often want to do a little preparation before navigation

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {

        // Get the new view controller using segue.destination.

        // Pass the selected object to the new view controller.

    }

    */

 

}

 

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐