iOS swift之Button詳解學(xué)習(xí)

{
    
    // 聲明按鈕是一個(gè)對象,是全局的
    var button1:UIButton!
    
    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        
        self.title = "六六六";
        
        button1 = UIButton.init(type: UIButtonType.custom)
//        button1 = UIButton(type:.custom)
//        button1 = UIButton.init(frame: CGRect(x: 10, y: 10, width: 100, height: 100))
        button1 = UIButton(type:.custom)
        // 默認(rèn)文字顏色為藍(lán)色,帶“!”圖標(biāo)按鈕
//        button1 = UIButton(type: .detailDisclosure)
//        button1 = UIButton(type: .infoDark)
//        button1 = UIButton(type: .infoLight)
//        button1 = UIButton(type: .contactAdd)
        button1.frame = CGRect(x: 100, y: 100, width: 100, height: 100)
        button1.backgroundColor = UIColor.white
        button1.backgroundColor = UIColor.init(white: 0.9, alpha: 1)
        
        button1.setBackgroundImage(UIImage(named:"num_1"), for: UIControlState.normal) // 普通狀態(tài)
        button1.setBackgroundImage(UIImage(named:"num_2"), for: UIControlState.highlighted) // 高亮狀態(tài)
        button1.setBackgroundImage(UIImage(named:"num_3"), for: UIControlState.selected) // 選中狀態(tài)
        
        // 設(shè)置按鈕不同狀態(tài)下的圖片
        button1.setImage(UIImage(named:"num_2"), for: UIControlState.normal)
        button1.setTitle("普通", for: UIControlState.normal)
        button1.setImage(UIImage(named:"num_1"), for: UIControlState.highlighted)
        button1.setTitle("高亮", for: UIControlState.highlighted)
        button1.setImage(UIImage(named:"num_3"), for: UIControlState.selected)
        button1.setTitle("選擇", for: UIControlState.selected)
        
        // 設(shè)置按鈕上文字的陰影
        button1.setTitleColor(UIColor.purple, for: UIControlState.normal)
        button1.setTitleColor(UIColor.red, for: UIControlState.highlighted)
        button1.setTitleColor(UIColor.darkGray, for: UIControlState.selected)
        
        button1.setTitleShadowColor(UIColor.cyan, for: UIControlState.normal)
        button1.setTitleShadowColor(UIColor.yellow, for: UIControlState.highlighted)
        button1.setTitleShadowColor(UIColor.blue, for: UIControlState.selected)
        
        button1.addTarget(self, action: #selector(buttonClick(button:)), for: UIControlEvents.touchUpInside)
        
        // 按鈕內(nèi)容的邊距(頂部、左邊、底部、右邊
        button1.contentEdgeInsets = UIEdgeInsetsMake(100, 0, 30, 0)
        
        // 按鈕上圖片的邊距
        button1.imageEdgeInsets = UIEdgeInsetsMake(10, 0, 20, 0)
        
        // 按鈕上文本框的邊距
        button1.titleEdgeInsets = UIEdgeInsetsMake(5, 0, 10, 0)
        
        // 按鈕長按狀態(tài)下去掉文字陰影
        button1.reversesTitleShadowWhenHighlighted = true
        
        // 高亮狀態(tài)下調(diào)整圖片
        button1.adjustsImageWhenHighlighted = true
        
        // 高亮狀態(tài)下變灰
        button1.showsTouchWhenHighlighted = true
        
        // 設(shè)置按鈕上的文字
        button1.titleLabel?.text = "逗比歡樂多";
        button1.imageView?.image = UIImage.init(named: "img111");
        
        //也可以按照這樣來創(chuàng)建按鈕
        self.view.addSubview(button1)
    }

    //按鈕的點(diǎn)擊事件的處理
    func buttonClick(button :UIButton){
        
        //反選效果
        button.isSelected = !button.isSelected;
        
        //獲取按鈕當(dāng)前文字
        print(button.currentTitle)
        
        /*
         按鈕當(dāng)前的一些屬性,注意:只能讀取,不能修改
         
         currentTitle//當(dāng)前文字
         currentTitleColor//當(dāng)前文字顏色
         currentTitleShadowColor//當(dāng)前文字陰影色
         
         currentImage//當(dāng)前圖片
         currentBackgroundImage//當(dāng)前背景圖片
         */
    }

    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
 
 
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

推薦閱讀更多精彩內(nèi)容