Xcode9学习笔记22 - UIButton控件的使用(边框、圆角、背景色、标题、类型)

Xcode9学习笔记22 - UIButton控件的使用(边框、圆角、背景色、标题、类型)_第1张图片

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        let bt1 = UIButton(type: UIButtonType.infoDark)//创建一个深色背景的提示信息按钮
        bt1.frame = CGRect(x: 138, y: 80, width: 40, height: 40)//设置按钮的坐标尺寸
        
        let bt2 = UIButton(type: UIButtonType.roundedRect)//创建一个普通的圆角按钮
        bt2.frame = CGRect(x: 80, y: 180, width: 150, height: 44)
        bt2.backgroundColor = UIColor.purple//设置按钮背景颜色
        bt2.tintColor = UIColor.yellow//设置按钮前景颜色
        bt2.setTitle("Tap me", for: UIControlState())//设置按钮的标题文字
        //给按钮绑定点击事件
        bt2.addTarget(self, action: #selector(ViewController.buttonTap(_:)), for: UIControlEvents.touchUpInside)
        
        let bt3 = UIButton(type: UIButtonType.roundedRect)
        bt3.backgroundColor = UIColor.brown
        bt3.tintColor = UIColor.white
        bt3.setTitle("Tap me", for: UIControlState())
        bt3.frame = CGRect(x: 80, y: 280, width: 150, height: 44)
        bt3.layer.masksToBounds = true//给按钮添加边框效果
        bt3.layer.cornerRadius = 10//设置按钮的圆角半径
        bt3.layer.borderWidth = 4//设置边框宽度
        bt3.layer.borderColor = UIColor.lightGray.cgColor//设置按钮层边框的颜色
        
        self.view.addSubview(bt1)//将3个按钮添加到当前视图控制器的根视图
        self.view.addSubview(bt2)
        self.view.addSubview(bt3)
    }
    
    @objc func buttonTap(_ button:UIButton) {
        let alert = UIAlertController(title: "Information", message: "UIButton Event.", preferredStyle: UIAlertControllerStyle.alert)//创建一个警告弹出窗口
        //创建一个按钮作为警告窗口的“确定”按钮
        let OKAction = UIAlertAction(title: "OK", style: UIAlertActionStyle.default, handler: nil)
        alert.addAction(OKAction)//将确定按钮添加到警告窗口中
        self.present(alert, animated: true, completion: nil)//在当前视图控制器中,展示提示窗口
    }


Xcode9学习笔记22 - UIButton控件的使用(边框、圆角、背景色、标题、类型)_第2张图片

你可能感兴趣的:(Xcode9学习笔记22 - UIButton控件的使用(边框、圆角、背景色、标题、类型))