
程式碼

------------------------- 從程式碼建立 Button -------------------------
建立 Button 變數
var myButton:UIButton = UIButton()
var myButton = UIButton()
將 Button 加到 畫面
------------------------- 設定Button Layout -------------------------

------------------------- Button成員 -------------------------
設定 Button 背景顏色
myButton.backgroundColor = UIColor.blue
設定 Button 文字內容
myButton.setTitle( "Blue" , for:UIControlState.normal)
設定 Button 文字內容 ( Button 按下時 )
myButton.setTitle( "Push" , for:UIControlStatae.highlighted )
設定 Button 文字顏色
myButton.setTitleColor( UIColor.yellow , for:UIControllerState.normal )
建立 Button 變數
var myButton:UIButton = UIButton()
var myButton = UIButton()
將 Button 加到 畫面
view.addSubview( myButton )
------------------------- 設定Button Layout -------------------------

------------------------- Button成員 -------------------------
設定 Button 背景顏色
myButton.backgroundColor = UIColor.blue
設定 Button 文字內容
myButton.setTitle( "Blue" , for:UIControlState.normal)
設定 Button 文字內容 ( Button 按下時 )
myButton.setTitle( "Push" , for:UIControlStatae.highlighted )
設定 Button 文字顏色
myButton.setTitleColor( UIColor.yellow , for:UIControllerState.normal )
設定 Button 文字顏色 ( Button 按下時 )
myButton.setTitleColor( UIColor.white , for:UIControlState.highlighted )
設定 Button 字型和大小
myButton.titleLabel.font = UIFont(name:"Helvetica", size:20)
------------------------- Button的layer 成員 -------------------------
設定 Button 邊框寬度
myButton.layer.border.width = 5
設定 Button 邊框顏色
myButton.layer.borderColor = UIColor.blue.cgColor
設定 Button 角度大小
myButton.layer.cornerRadius = 10.0
開啟 Button 遮罩功能 (可省略)
myButton.layer.masksToBounds = true
myButton.setTitleColor( UIColor.white , for:UIControlState.highlighted )
設定 Button 字型和大小
myButton.titleLabel.font = UIFont(name:"Helvetica", size:20)
------------------------- Button的layer 成員 -------------------------
設定 Button 邊框寬度
myButton.layer.border.width = 5
設定 Button 邊框顏色
myButton.layer.borderColor = UIColor.blue.cgColor
設定 Button 角度大小
myButton.layer.cornerRadius = 10.0
開啟 Button 遮罩功能 (可省略)
myButton.layer.masksToBounds = true
------------------------- Button的Action -------------------------
設定 Button 標籤
myButton.tag = 1
按下 Button 呼叫事件,當 Button 按下後離開時呼叫事件
myButton.addTarget( self , action:#selector(ViewController.myBtnTouchUpInside) , for: .touchUpInside )
自訂事件,印出 Button 標籤
func myBtnTouchUpInside( sender:UIButton! )
{
print(sender.tag)
}
------------------------- 其它 -------------------------
設定 Button 座標和大小
myButton.frame = CGRect( x:0 , y:0 , width:50 , height:50 )
設定 Button 中心點的座標
myButton.layer.position = CGPoint( x:view.frame.width/2 , y:view.frame.height/2-20 )
設定 Button 中心點的座標
myButton.layer.position = CGPoint( x:view.frame.width/2 , y:view.frame.height/2-20 )
簡化後的程式碼

沒有留言:
張貼留言