Swift01-设置button

前端之家收集整理的这篇文章主要介绍了Swift01-设置button前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

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可以简化为
 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")
    }
原文链接:https://www.f2er.com/swift/323275.html

猜你在找的Swift相关文章