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, 200, 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")
    }
發佈了55 篇原創文章 · 獲贊 10 · 訪問量 12萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章