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("未选中按钮") } }
原文地址:https://www.jb51.cc/swift/322930.html
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。