前端之家收集整理的这篇文章主要介绍了
swift中UIButton的使用,
前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
let button = UIButton(type: UIButtonType.Custom)
// 添加到父视图,并设置frame
self.view.addSubview(button)
button.frame = CGRectMake(10.0,10.0,200.0,40.0)
// 背景属性设置
button.backgroundColor = UIColor.yellowColor()
// button.setBackgroundImage(UIImage(named: "normalImage"),forState: UIControlState.Normal)
// button.setBackgroundImage(UIImage(named: "hightImage"),forState: UIControlState.Highlighted)
// 图标设置
button.setImage(UIImage(named: "normalImage"),forState: UIControlState.Normal)
button.setImage(UIImage(named: "hightImage"),forState: UIControlState.Highlighted)
button.imageEdgeInsets = UIEdgeInsetsMake(0.0,-50.0,0.0,0.0)
// 标题设置
button.setTitle("button",forState: UIControlState.Normal)
button.setTitleColor(UIColor.blackColor(),forState: UIControlState.Normal)
button.setTitleColor(UIColor.redColor(),forState: UIControlState.Highlighted)
button.setTitleShadowColor(UIColor.greenColor(),forState: UIControlState.Normal)
button.titleLabel?.font = UIFont(name: "GillSans",size: 20.0)
button.titleEdgeInsets = UIEdgeInsetsMake(0.0,-50.0)
button.selected = false
button.enabled = true
button.userInteractionEnabled = true
// 响应事件
button.addTarget(self,action: Selector("buttonClick:"),forControlEvents: UIControlEvents.TouchUpInside)
// 响应事件
func buttonClick(button:UIButton) -> Void
{
button.selected = !button.selected
if button.selected == true
{
print("选中按钮")
let buttonTitle = button.titleLabel?.text
print(buttonTitle)
}
else
{
print("未选中按钮")
}
}