swift中UIButton的使用
发布时间:2020-12-14 06:42:34 所属栏目:百科 来源:网络整理
导读:let button = UIButton(type: UIButtonType.Custom) // 添加到父视图,并设置frameself.view.addSubview(button)button.frame = CGRectMake(10.0,10.0,200.0,40.0) // 背景属性设置button.backgroundColor = UIColor.yellowColor()// button.setBackgroundIm
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("未选中按钮") } } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |