Swift01-设置button
发布时间:2020-12-14 06:49:29 所属栏目:百科 来源:网络整理
导读:override func viewDidLoad() { super.viewDidLoad() // var button: UIButton = UIButton ( type :. ContactAdd ) var button: UIButton = UIButton ( type :. InfoDark ); //设置按钮位置和大小 button.frame = CGRectMake(10,200,100,30) //设置frame可以
override func viewDidLoad() { // var button:UIButton = UIButton(type:.ContactAdd)
var button:UIButton = UIButton(type:.InfoDark);
//设置按钮位置和大小 button.frame = CGRectMake(10,200,100,30)
//设置frame可以简化为 var button2 = UIButton(frame:CGRectMake(10,250,100))
//按钮文字以及文字颜色的设置 button2.setTitle("button2",forState: UIControlState.Normal)
button2.setTitleColor(UIColor.blueColor(),forState: UIControlState.Normal)
//添加button2到self.view self.view.addSubview(button2);
button.setTitle("按钮",forState: UIControlState.Normal); //设置按钮图片 button2.setImage(UIImage(named:"icon"),forState: UIControlState.Normal)
self.view .addSubview(button); //设置背景颜色 // button.backgroundColor = UIColor.redColor()
//设置button点击事件(这里跟oc有点区别 oc是@selector) button2.addTarget(self,action: #selector(buttonPressed),forControlEvents: UIControlEvents.TouchUpInside)
} // 不传递触摸对象(即点击的按钮) func buttonPressed(){
print("tapped")
}
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |