UIButton Swift

1,按钮的创建
(1)按钮有下面四种类型:

UIButtonType.ContactAdd:前面带“+”图标按钮,默认文字颜色为蓝色,有触摸时的高亮效果
UIButtonType.DetailDisclosure:前面带“!”图标按钮,默认文字颜色为蓝色,有触摸时的高亮效果
UIButtonType.System:前面不带图标,默认文字颜色为蓝色,有触摸时的高亮效果
UIButtonType.Custom:定制按钮,前面不带图标,默认文字颜色为白色,无触摸时的高亮效果
UIButtonType.InfoDark:为感叹号“!”圆形按钮
UIButtonType.InfoLight:为感叹号“!”圆形按钮

 let btn=UIButton(type: .ContactAdd)

 

(2)对于Custom定制类型按钮,代码可简化为:

   let button=UIButton(frame: CGRect(x: 0, y: 0, 100, height: 100))

        button.backgroundColor=UIColor.purpleColor()

       // button.frame=CGRectMake(0, 0, 100, 100)

        button.titleLabel!.font=UIFont.systemFontOfSize(20)//文字的大小

        button.setTitle("Swift", forState:UIControlState.Normal)

        button.setTitle("Button", forState: UIControlState.Highlighted)

    

        button.setTitleColor(.redColor(), forState: UIControlState.Normal)//普通状态下文字的颜色

        button.setTitleColor(UIColor.yellowColor(), forState: UIControlState.Highlighted)

        button.setTitleShadowColor(UIColor.whiteColor(), forState: UIControlState.Normal)//普通状态下文字阴影的颜色

        button.setTitleShadowColor(UIColor.blackColor(), forState: UIControlState.Highlighted)

        button.setImage(UIImage(named: "Icon-72"), forState: UIControlState.Normal)//设置图标

        button.adjustsImageWhenHighlighted=false//使触摸模式下按钮也不会变暗

        

        button.setBackgroundImage(UIImage(named: "1.jpg"), forState: UIControlState.Normal)//背景图片

        button.addTarget(self, action: Selector("clicekdBtn:"), forControlEvents: .TouchUpInside)

        self.view.addSubview(button)

    }

    func clicekdBtn(button:UIButton){

    print(button.titleForState(UIControlState.Normal))

    }

原文地址:https://www.cnblogs.com/woaixixi/p/4995370.html