UI控件之UIButton

//  Created by Catherine on 2017/8/28.

//  Copyright © 2017 Catherine. All rights reserved.

//


import UIKit


class ViewController: UIViewController {


    override func viewDidLoad() {

        super.viewDidLoad()

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

        //创建并设置button的类型

        let button : UIButton = UIButton(type: UIButtonType.custom)

        button.frame = CGRect(x: 100, y: 100, width: 100, height: 100)

        button.backgroundColor = UIColor.red

        self.view.addSubview(button)

        //添加事件

        button.addTarget(self, action: #selector(click(btn:)), for: .touchUpInside)

        //选择何时触发

        //设置按钮的标题

        button.setTitle("按钮", for: .normal)

        //设置按钮的图片

        //背景图片(按钮在上面)title在图片上面

        //button.setBackgroundImage(UIImage("l"), for: .normal)

        //title和图片是并列的

        //button.setImage(<#T##image: UIImage?##UIImage?#>, for: <#T##UIControlState#>)//state是高亮状态

        //设置button内容区域的偏移量

        button.contentEdgeInsets = UIEdgeInsetsMake(0, 10, 0, 0)

        //开启按钮的点击视觉效果   按下去有一圈白晕效果

        button.showsTouchWhenHighlighted = true

        //设置高亮状态的按钮标题  按下去变成“new”三个字

        button.setTitle("new", for: .highlighted)

        //设置title颜色 title阴影

        button.setTitleColor(UIColor.brown, for: .normal)

        //button.setTitleShadowColor(<#T##color: UIColor?##UIColor?#>, for: <#T##UIControlState#>)

    }

    //button对象进行了传递

    func click(btn:UIButton){

        print("按钮被点击了")

        btn.backgroundColor = UIColor(red: (CGFloat)(arc4random()%255)/255, green: (CGFloat)(arc4random()%255)/255, blue: (CGFloat)(arc4random()%255)/255, alpha: 1)

        

        

    }

    override func didReceiveMemoryWarning() {

        super.didReceiveMemoryWarning()

        // Dispose of any resources that can be recreated.

    }



}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值