swift中UIButton的简单使用

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

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

1.UIButton的创建


a.不带样式的:

let btn: UIButton = UIButton()

b.带样式的

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

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

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

用法如下:

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)

例子如下:

  1. //创建一个图片一个文字的按钮
  2. let btn2: UIButton = UIButton(type: .Custom)
  3. btn2.frame = CGRectMake(50,100,120,35)
  4. btn2.setImage(UIImage(named: "1"),forState: .Normal)
  5. btn2.backgroundColor = UIColor.blackColor()
  6. btn2.titleLabel?.font = UIFont.systemFontOfSize(20)
  7. btn2.imageView?.contentMode = UIViewContentMode.ScaleAspectFit
  8. btn2.setTitle("图片按钮",forState: .Normal)
  9. //偏移量,分别为上下左右
  10. btn2.imageEdgeInsets = UIEdgeInsetsMake(0,-50,0)
  11. btn2.titleEdgeInsets = UIEdgeInsetsMake(0,-80,5)
  12. btn2.setTitleColor(UIColor.whiteColor(),forState: .Normal)
  13. btn2.adjustsImageWhenHighlighted = false
  14. 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){}

写的代码如下:

  1. func initButtonView() {
  2. //创建有状态的按钮
  3. let btn: UIButton = UIButton(type: .Custom)
  4. btn.frame = CGRectMake(50,20,30)
  5. btn.setTitle("点击按钮",forState: .Selected)
  6. btn.setTitle("未点击",forState: .Normal)
  7. btn.backgroundColor = UIColor.blackColor()
  8. btn.addTarget(self,action: "clickBtn:",forControlEvents: .TouchUpInside)
  9. self.view .addSubview(btn)
  10. //创建无状态的按钮
  11. let btn1: UIButton = UIButton()
  12. btn1.frame = CGRectMake(50,60,30)
  13. btn1.setTitle("Normal",forState: .Normal)
  14. btn1.backgroundColor = UIColor.blueColor()
  15. self.view.addSubview(btn1)
  16. //创建一个图片一个文字的按钮
  17. let btn2: UIButton = UIButton(type: .Custom)
  18. btn2.frame = CGRectMake(50,forState: .Normal)
  19. btn2.adjustsImageWhenHighlighted = false
  20. self.view.addSubview(btn2)
  21. //创建禁止按钮
  22. let btn3: UIButton = UIButton(type: .Custom)
  23. btn3.frame = CGRectMake(50,140,35)
  24. btn3.setTitle("点击按钮",forState: .Highlighted)
  25. btn3.setTitle("禁止按钮",forState: .Normal)
  26. btn3.enabled = false //禁止按钮,默认为true
  27. btn3.setTitleColor(UIColor.redColor(),forState: .Disabled)
  28. btn3.backgroundColor = UIColor.purpleColor()
  29. self.view.addSubview(btn3)
  30. //创建圆角按钮
  31. let btn4: UIButton = UIButton(type: .Custom)
  32. btn4.frame = CGRectMake(50,180,35)
  33. btn4.backgroundColor = UIColor.blackColor()
  34. btn4.setTitle("圆角按钮 ",forState: .Normal)
  35. btn4.setTitleColor(UIColor.whiteColor(),forState: .Normal)
  36. btn4.layer.cornerRadius = 8
  37. self.view.addSubview(btn4)
  38. //部分圆角按钮,主要利用layer的mask属性,在tongguoCAShaperLayer和UIBezierPath来画
  39. let btn5: UIButton = UIButton(type: .Custom)
  40. btn5.frame = CGRectMake(50,220,35)
  41. btn5.backgroundColor = UIColor.blackColor()
  42. btn5.setTitle("部分圆角按钮",forState: .Normal)
  43. btn5.setTitleColor(UIColor.whiteColor(),forState: .Normal)
  44. let shape: CAShapeLayer = CAShapeLayer()
  45. let bepath: UIBezierPath = UIBezierPath(roundedRect: btn5.bounds,byRoundingCorners: [UIRectCorner.TopRight,UIRectCorner.TopLeft,UIRectCorner.BottomLeft],cornerRadii: CGSize(width: 8,height: 8))
  46. UIColor.blackColor().setStroke()
  47. shape.path = bepath.CGPath
  48. btn5.layer.mask = shape
  49. self.view.addSubview(btn5)
  50. //创建带边框的按钮
  51. let btn6: UIButton = UIButton(type: .Custom)
  52. btn6.frame = CGRectMake(50,260,35)
  53. btn6.setTitle("边框按钮",forState: .Normal)
  54. btn6.setTitleColor(UIColor.blackColor(),forState: .Normal)
  55. btn6.layer.borderColor = UIColor.blackColor().CGColor
  56. btn6.layer.borderWidth = 1
  57. btn6.layer.cornerRadius = 8
  58. self.view.addSubview(btn6)
  59. //显示提示信息的UILabel
  60. labelText = UILabel()
  61. labelText.frame = CGRectMake(50,300,44)
  62. labelText.textColor = UIColor.orangeColor()
  63. self.view.addSubview(labelText)
  64. let btns: UIButton = UIButton(type: .Custom)
  65. btns.frame = CGRectMake(<#T##x: CGFloat##CGFloat#>,<#T##y: CGFloat##CGFloat#>,<#T##width: CGFloat##CGFloat#>,<#T##height: CGFloat##CGFloat#>)
  66. btn.setTitle("按钮",forState: .Normal)
  67. btn.setTitleColor(UIColor.whiteColor(),forState: .Normal)
  68. btn.backgroundColor = UIColor.blackColor()
  69. btn.setBackgroundImage(UIImage(named: "1"),forState: .Normal)
  70. btn.imageEdgeInsets = UIEdgeInsetsMake(<#T##top: CGFloat##CGFloat#>,<#T##left: CGFloat##CGFloat#>,<#T##bottom: CGFloat##CGFloat#>,<#T##right: CGFloat##CGFloat#>)
  71. btn.titleEdgeInsets = UIEdgeInsetsMake(<#T##top: CGFloat##CGFloat#>,<#T##right: CGFloat##CGFloat#>)
  72. btn.addTarget(self,action: "click",forControlEvents: .TouchUpInside)
  73. }
  74. func clickBtn(sender: UIButton){
  75. sender.selected = !sender.selected
  76. labelText.text = "点击了按钮"
  77. }

效果图如下:

猜你在找的Swift相关文章