UI控件之UIButton

来源:互联网 发布:win10 办公软件 编辑:程序博客网 时间:2024/05/18 01:09

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

//  Copyright © 2017 Catherine. All rights reserved.

//


import UIKit


class ViewController: UIViewController {


    overridefunc 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)

        

        

    }

    overridefunc didReceiveMemoryWarning() {

        super.didReceiveMemoryWarning()

        // Dispose of any resources that can be recreated.

    }



}


原创粉丝点击