swift中UIButton的简单使用

前端之家收集整理的这篇文章主要介绍了swift中UIButton的简单使用前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

UIButton是我们经常用的一个控件,它是继承自UIControl的,下面就总结一下相关属性用法

1.UIButton的创建


a.不带样式的:

let btn: UIButton = UIButton()

b.带样式的

let btns:UIButton =UIButton(type: UIButtonType);

其中UIButtonType是一个枚举如下所示:

publicenum UIButtonType :Int {
    case Custom // no button type 自定义样式按钮
    @available(iOS 7.0,*)
    case System // standard system button	//系统的样式按钮
    case DetailDisclosure
    case InfoLight
    case InfoDark
   case ContactAdd//联系人按钮
    public staticvar RoundedRect: UIButtonType { get }// Deprecated,use UIButtonTypeSystem instead
}

用法如下:

let btn: UIButton = UIButton(type: .Custom)

2.UIButton设置字内容和颜色

btn.setTitle("按钮",forState: .Normal)

btn.setTitleColor(UIColor.whiteColor(),forState: .Normal)


3.UIButton设置背景颜色和背景图片

btn.backgroundColor =UIColor.blackColor()

btn.setBackgroundImage(UIImage(named:"1"),forState: .Normal)


4.UIButton设置字体大小

btn.titleLabel?.font =UIFont.systemFontOfSize(20)


5.禁用UIButton

btn.enabled =false //禁止按钮,默认为true


6.设置圆角

btn.layer.cornerRadius = 8


7.设置背景图片为圆角(因为我们直接设置UIButton圆角时,图片不会变为圆角)

buttonImage.setImage(UIImage(named:"1"),forState: UIControlState.Normal)

//设置背景图片为圆角

buttonImage.imageView?.layer.cornerRadius = 50


8.在UIButton上添加图片文字,有时需要我们调整,此时需要:

方向为逆时针方向,上、左、下、右依次去设置的

btn.imageEdgeInsets =UIEdgeInsetsMake(top: CGFloat,left: CGFloat,bottom: CGFloat,right: CGFloat)

btn.titleEdgeInsets =UIEdgeInsetsMake(top: CGFloat,right: CGFloat)

例子如下:

        //创建一个图片一个文字的按钮
        let btn2: UIButton = UIButton(type: .Custom)
        btn2.frame = CGRectMake(50,100,120,35)
        btn2.setImage(UIImage(named: "1"),forState: .Normal)
        btn2.backgroundColor = UIColor.blackColor()
        btn2.titleLabel?.font = UIFont.systemFontOfSize(20)
        btn2.imageView?.contentMode = UIViewContentMode.ScaleAspectFit
        btn2.setTitle("图片按钮",forState: .Normal)
        //偏移量,分别为上下左右
        btn2.imageEdgeInsets = UIEdgeInsetsMake(0,-50,0)
        btn2.titleEdgeInsets = UIEdgeInsetsMake(0,-80,5)
        btn2.setTitleColor(UIColor.whiteColor(),forState: .Normal)
        btn2.adjustsImageWhenHighlighted = false
        self.view.addSubview(btn2)
9 .添加按钮的点击事件

第一种是不带参数的,第二种是带参数的

btn.addTarget(self,action:"click",forControlEvents: .TouchUpInside)

btn.addTarget(self,action:"clicks:",forControlEvents: .TouchUpInside)

第一种:

func click(){}

第二种:

func clicks(sender:UIButton){}

写的代码如下:

func initButtonView() {
        //创建有状态的按钮
        let btn: UIButton = UIButton(type: .Custom)
        btn.frame = CGRectMake(50,20,30)
        btn.setTitle("点击按钮",forState: .Selected)
        btn.setTitle("未点击",forState: .Normal)
        btn.backgroundColor = UIColor.blackColor()
        btn.addTarget(self,action: "clickBtn:",forControlEvents: .TouchUpInside)
        self.view .addSubview(btn)
        
        //创建无状态的按钮
        let btn1: UIButton = UIButton()
        btn1.frame = CGRectMake(50,60,30)
        btn1.setTitle("Normal",forState: .Normal)
        btn1.backgroundColor = UIColor.blueColor()
        self.view.addSubview(btn1)
        
        //创建一个图片一个文字的按钮
        let btn2: UIButton = UIButton(type: .Custom)
        btn2.frame = CGRectMake(50,forState: .Normal)
        btn2.adjustsImageWhenHighlighted = false
        self.view.addSubview(btn2)
        
        //创建禁止按钮
        let btn3: UIButton = UIButton(type: .Custom)
        btn3.frame = CGRectMake(50,140,35)
        btn3.setTitle("点击按钮",forState: .Highlighted)
        btn3.setTitle("禁止按钮",forState: .Normal)
        btn3.enabled = false    //禁止按钮,默认为true
        btn3.setTitleColor(UIColor.redColor(),forState: .Disabled)
        btn3.backgroundColor = UIColor.purpleColor()
        self.view.addSubview(btn3)
        
        //创建圆角按钮
        let btn4: UIButton = UIButton(type: .Custom)
        btn4.frame = CGRectMake(50,180,35)
        btn4.backgroundColor = UIColor.blackColor()
        btn4.setTitle("圆角按钮 ",forState: .Normal)
        btn4.setTitleColor(UIColor.whiteColor(),forState: .Normal)
        btn4.layer.cornerRadius = 8
        self.view.addSubview(btn4)
        
        //部分圆角按钮,主要利用layer的mask属性,在tongguoCAShaperLayer和UIBezierPath来画
        let btn5: UIButton = UIButton(type: .Custom)
        btn5.frame = CGRectMake(50,220,35)
        btn5.backgroundColor = UIColor.blackColor()
        btn5.setTitle("部分圆角按钮",forState: .Normal)
        btn5.setTitleColor(UIColor.whiteColor(),forState: .Normal)
        let shape: CAShapeLayer = CAShapeLayer()
        let bepath: UIBezierPath = UIBezierPath(roundedRect: btn5.bounds,byRoundingCorners: [UIRectCorner.TopRight,UIRectCorner.TopLeft,UIRectCorner.BottomLeft],cornerRadii: CGSize(width: 8,height: 8))
        UIColor.blackColor().setStroke()
        shape.path = bepath.CGPath
        btn5.layer.mask = shape
        self.view.addSubview(btn5)
        
        //创建带边框的按钮
        let btn6: UIButton = UIButton(type: .Custom)
        btn6.frame = CGRectMake(50,260,35)
        btn6.setTitle("边框按钮",forState: .Normal)
        btn6.setTitleColor(UIColor.blackColor(),forState: .Normal)
        btn6.layer.borderColor = UIColor.blackColor().CGColor
        btn6.layer.borderWidth = 1
        btn6.layer.cornerRadius = 8
        self.view.addSubview(btn6)
        
        //显示提示信息的UILabel
        labelText = UILabel()
        labelText.frame = CGRectMake(50,300,44)
        labelText.textColor = UIColor.orangeColor()
        self.view.addSubview(labelText)
        
        let btns: UIButton = UIButton(type: .Custom)
        btns.frame = CGRectMake(<#T##x: CGFloat##CGFloat#>,<#T##y: CGFloat##CGFloat#>,<#T##width: CGFloat##CGFloat#>,<#T##height: CGFloat##CGFloat#>)
        btn.setTitle("按钮",forState: .Normal)
        btn.setTitleColor(UIColor.whiteColor(),forState: .Normal)
        btn.backgroundColor = UIColor.blackColor()
        btn.setBackgroundImage(UIImage(named: "1"),forState: .Normal)
        btn.imageEdgeInsets = UIEdgeInsetsMake(<#T##top: CGFloat##CGFloat#>,<#T##left: CGFloat##CGFloat#>,<#T##bottom: CGFloat##CGFloat#>,<#T##right: CGFloat##CGFloat#>)
        btn.titleEdgeInsets = UIEdgeInsetsMake(<#T##top: CGFloat##CGFloat#>,<#T##right: CGFloat##CGFloat#>)
        
        btn.addTarget(self,action: "click",forControlEvents: .TouchUpInside)
    }
    
    func clickBtn(sender: UIButton){
        
        sender.selected = !sender.selected
        labelText.text = "点击了按钮"
    }

效果图如下:

猜你在找的Swift相关文章