码迷,mamicode.com
首页 > 编程语言 > 详细

UIButton Swift

时间:2015-11-25 18:46:32      阅读:138      评论:0      收藏:0      [点我收藏+]

标签:

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, width: 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))

    }

UIButton Swift

标签:

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!