1. 程式人生 > >Swift-按鈕(UIButton)詳解

Swift-按鈕(UIButton)詳解

//宣告按鈕的一個物件,是全域性的

var button1 :UIButton!

overridefunc viewDidLoad() {

super.viewDidLoad()

//以自定義型別建立按鈕

button1 =UIButton.init(type:UIButtonType.Custom)

//也可以這樣來建立按鈕

//        let button2 = UIButton(type:.Custom)

//        let button3 = UIButton(type:.Custom)

//        let button4 = UIButton.init(frame: CGRectMake(10, 10, 100, 100))

//另外,按鈕也可以用以下型別建立

//預設文字顏色為藍色,不帶圖示

button1 =UIButton(type:.System)

//預設文字顏色為藍色,“!”圖示按鈕

button1 =UIButton(type:.DetailDisclosure)

//感嘆號“!”圓形按鈕

button1 =UIButton(type:.InfoLight)

//感嘆號“!"圓形按鈕

button1 =UIButton(type:.InfoDark)

//預設文字為藍色,帶“+”圖示按鈕

button1 =UIButton(type:.ContactAdd)

//設定按鈕的尺寸

button1

.frame =CGRectMake(100,100, 100,100)

//設定按鈕的背景色

button1.backgroundColor =UIColor.whiteColor()

button1.backgroundColor =UIColor.init(white:0.9, alpha: 1)

//設定按鈕的背景圖片

//普通狀態

button1.setBackgroundImage(UIImage(named:"icon1"), forState: UIControlState.Normal)

//高亮狀態

button1.setBackgroundImage(UIImage(named:"icon2"

), forState: UIControlState.Highlighted)

//選中狀態

button1.setBackgroundImage(UIImage(named:"icon3"), forState: UIControlState.Selected)

//設定按鈕的文字和文字的顏色

//普通狀態

button1.setTitle("普通", forState: UIControlState.Normal)

button1.setTitleColor(UIColor.blueColor(), forState: UIControlState.Normal)

//高亮狀態

button1.setTitle("高亮", forState: UIControlState.Highlighted)

button1.setTitleColor(UIColor.redColor(), forState: UIControlState.Highlighted)

//選中狀態

button1.setTitle("選中", forState: UIControlState.Selected)

button1.setTitleColor(UIColor.blackColor(), forState: UIControlState.Selected)

//設定按鈕不同狀態下的圖片

//普通狀態

button1.setImage(UIImage(named:"icon1"), forState: UIControlState.Normal)

//高亮狀態

button1.setImage(UIImage(named:"icon2"), forState: UIControlState.Highlighted)

//選中狀態

button1.setImage(UIImage(named:"icon3"), forState: UIControlState.Selected)

//設定按鈕上文字的陰影

//普通狀態下

button1.setTitleShadowColor(UIColor.greenColor(), forState: UIControlState.Normal)

//高亮狀態下

button1.setTitleShadowColor(UIColor.yellowColor(), forState: UIControlState.Highlighted)

//選中狀態下

button1.setTitleShadowColor(UIColor.purpleColor(), forState: UIControlState.Selected)

//設定tag

button1.addTarget(self, action:#selector(buttonClick(_:)), forControlEvents:UIControlEvents.TouchUpInside)

//按鈕內容的邊距(頂部,左邊,底部,左邊)

button1.contentEdgeInsets =UIEdgeInsetsMake(100,0, 30,0)

//按鈕上圖片的邊距

button1.imageEdgeInsets =UIEdgeInsetsMake(10,0, 20,0)

//按鈕上文字框的邊距

button1.titleEdgeInsets =UIEdgeInsetsMake(5,0, 10,0)

//按鈕長按狀態下去掉文字陰影

button1.reversesTitleShadowWhenHighlighted =true

//高亮狀態下調整圖片

button1.adjustsImageWhenHighlighted =true

//高亮狀態下變灰

button1.showsTouchWhenHighlighted =true

//設定按鈕上的文字

button1.titleLabel?.text ="請您關注多多關注"

//設定按鈕上的圖片

button1.imageView?.image =UIImage.init(named:"icon1")

//新增按鈕

self.view.addSubview(button1)

    }

//按鈕的點選事件的處理

func buttonClick(button :UIButton){

//反選效果

        button.selected = !button.selected;

//獲取按鈕當前文字

print(button.currentTitle)

/*

按鈕當前的一些屬性,注意:只能讀取,不能修改

         currentTitle//當前文字

         currentTitleColor//當前文字顏色

         currentTitleShadowColor//當前文字陰影色

         currentImage//當前圖片

         currentBackgroundImage//當前背景圖片

         */

    }