遲到的Swift入門(mén) - UIButton

UIButton 教程

1.UIButton基本操作

1.創(chuàng)建按鈕

let btn: UIButton = UIButton()//沒(méi)有樣式
let btns:UIButton =UIButton(type: UIButtonType)//有樣式
let button = UIButton(frame:CGRect(x:10, y:150, width:100, height:30))//簡(jiǎn)化創(chuàng)建方式

** UIButtonType有以下類(lèi)型 **

public enum UIButtonType : Int {
    case custom // no button type
    @available(iOS 7.0, *)
    case system // standard system button
    case detailDisclosure
    case infoLight
    case infoDark
    case contactAdd
    public static var roundedRect: UIButtonType { get } // Deprecated, use UIButtonTypeSystem instead
}
//使用
let  btn: UIButton = UIButton(type: .Custom)

** UIButton狀態(tài)類(lèi)型 **

/**
     Normal          (默認(rèn)狀態(tài))
     Highlighted    (高亮狀態(tài))點(diǎn)擊按鈕不放
     Disabled       (使能狀態(tài))就是是否可用狀態(tài)-->禁用的狀態(tài)才會(huì)顯現(xiàn)
     Selected       (選中狀態(tài))通過(guò)selected屬性設(shè)置
 */

2、UIButton設(shè)置字內(nèi)容和顏色

//顯示文字
button1.setTitle("普通狀態(tài)", for: .normal)
button1.setTitle("高粱狀態(tài)", for: .highlighted)
button1.setTitle("禁用狀態(tài)", for: .disabled)
//顯示文字顏色
button1.setTitleColor(UIColor.red, for: .normal)
button1.setTitleColor(UIColor.blue, for: .highlighted)
button1.setTitleColor(UIColor.cyan, for: .selected)
button1.setTitleColor(UIColor.cyan, for: .disabled)
//陰影文字顏色設(shè)置
button1.setTitleShadowColor(UIColor.cyan, for: .normal)
button1.setTitleShadowColor(UIColor.green, for: .highlighted)
button1.setTitleShadowColor(UIColor.brown, for: .disabled)
button1.setTitleShadowColor(UIColor.darkGray, for: .selected)

3.UIButton設(shè)置背景顏色和背景圖片

//背景顏色
button2.backgroundColor = UIColor.orange
//背景圖片    
button4.setBackgroundImage(UIImage(named:"XXX"), for: .normal)

4.UIButton設(shè)置字體大小

button.titleLabel?.font = UIFont.systemFont(ofSize: 12)

5.禁用UIButton

button.isEnabled = false
button.isEnabled = true

6.設(shè)置圓角

button.layer.cornerRadius = 5
button.layer.masksToBounds = true

7.設(shè)置邊框?qū)挾?顏色

button.layer.borderWidth = 2
button.layer.borderColor = UIColor.red.cgColor

8.設(shè)置背景圖片為圓角

buttonImage.setImage(UIImage(named:"1") , forState: UIControlState.Normal)
//設(shè)置背景圖片為圓角
buttonImage.imageView?.layer.cornerRadius = 50

默認(rèn)情況下按鈕會(huì)被渲染成單一顏色;系統(tǒng)藍(lán)
button.setImage(UIImage(named:"icon1"),forState:.Normal)  //設(shè)置圖標(biāo)
button.adjustsImageWhenHighlighted=false //使觸摸模式下按鈕也不會(huì)變暗(半透明)
button.adjustsImageWhenDisabled=false //使禁用模式下按鈕也不會(huì)變暗(半透明)

也可以設(shè)置成保留圖標(biāo)原來(lái)的顏色
let iconImage = UIImage(named:"icon2")?.withRenderingMode(.alwaysOriginal)
button.setImage(iconImage, for:.normal)  //設(shè)置圖標(biāo)
button.adjustsImageWhenHighlighted = false //使觸摸模式下按鈕也不會(huì)變暗(半透明)
button.adjustsImageWhenDisabled = false //使禁用模式下按鈕也不會(huì)變暗(半透明)

9.UIButton上圖片和文字調(diào)整

? UIButton上添加圖片和文字苟径,有時(shí)需要我們調(diào)整方向?yàn)槟鏁r(shí)針?lè)较蛘ㄏ纤觥⒆笈埠濉⑾滤骄凇⒂乙来稳ピO(shè)置的

btn.imageEdgeInsets =UIEdgeInsetsMake(top: CGFloat, left: CGFloat, bottom: CGFloat, right: CGFloat)

btn.titleEdgeInsets =UIEdgeInsetsMake(top: CGFloat, left: CGFloat, bottom: CGFloat, right: CGFloat)

實(shí)例如下:

//創(chuàng)建一個(gè)圖片一個(gè)文字的按鈕  
let btn2: UIButton = UIButton(type: .Custom)  
btn2.frame = CGRectMake(50, 100, 120, 35)  
btn2.setImage(UIImage(named: "1"), forState: .Normal)  
btn2.backgroundColor = UIColor.blackColor()  
btn2.titleLabel?.font = UIFont.systemFontOfSize(20)  
btn2.imageView?.contentMode = UIViewContentMode.ScaleAspectFit  
btn2.setTitle("圖片按鈕", forState: .Normal)  
//偏移量移怯,分別為上下左右  
btn2.imageEdgeInsets = UIEdgeInsetsMake(0, -50, 0, 0)  
btn2.titleEdgeInsets = UIEdgeInsetsMake(0, -80, 0, 5)  
btn2.setTitleColor(UIColor.whiteColor(), forState: .Normal)  
btn2.adjustsImageWhenHighlighted = false  
self.view.addSubview(btn2) 

10.添加按鈕的點(diǎn)擊事件

按鈕的觸摸時(shí)間有以下類(lèi)型

touchDown:?jiǎn)吸c(diǎn)觸摸按下事件香璃,點(diǎn)觸屏幕
touchDownRepeat:多點(diǎn)觸摸按下事件,點(diǎn)觸計(jì)數(shù)大于1舟误,按下第2葡秒、3或第4根手指的時(shí)候
touchDragInside:觸摸在控件內(nèi)拖動(dòng)時(shí)
touchDragOutside:觸摸在控件外拖動(dòng)時(shí)
touchDragEnter:觸摸從控件之外拖動(dòng)到內(nèi)部時(shí)
touchDragExit:觸摸從控件內(nèi)部拖動(dòng)到外部時(shí)
touchUpInside:在控件之內(nèi)觸摸并抬起事件
touchUpOutside:在控件之外觸摸抬起事件
touchCancel:觸摸取消事件,即一次觸摸因?yàn)榉派咸嗍种付蝗∠兑纾蛘唠娫?huà)打斷
button1.addTarget(self,action:#selector(methodName), for: .touchUpInside)
button1.addTarget(self, action:#selector(someMethod(button:)), for:.touchUpInside)
//上
    func methodName() {
        print("tapped")
    }

//下
    func someMethod(button:UIButton) {
        print("你是誰(shuí)啊眯牧,其實(shí)就是一個(gè)按鈕")
    }

2.自定義操作

1.UIButton的圖片文字布局

創(chuàng)建一個(gè)按鈕且其同時(shí)擁有文字和圖片屬性,會(huì)按照系統(tǒng)的默認(rèn)樣式(左圖片赖草,右文字)顯示学少。但是有的時(shí)候,我們會(huì)遇到其他的設(shè)計(jì)需求秧骑,比如:左文字有圖片版确、上文字下圖片、上圖片下文字等乎折。這個(gè)時(shí)候就需要我們自定義按鈕的顯示樣式來(lái)滿(mǎn)足復(fù)雜多變的設(shè)計(jì)需求了绒疗。
我自定義了一個(gè)按鈕的extension來(lái)滿(mǎn)足以上的功能,代碼如下:

import Foundation
import UIKit

enum ButtonLayout {
    case leftImage
    case rightImage
    case topImage
    case bottomImage
}

extension UIButton {
    
    func setLayoutType(type: ButtonLayout){
        let image: UIImage? = self.imageView?.image
        switch type {
        case .leftImage:
            print("系統(tǒng)默認(rèn)的方式")
        case .rightImage:
            self.imageEdgeInsets = UIEdgeInsets(top:0, left: (self.titleLabel?.frame.size.width)!, bottom: 0, right:-(self.titleLabel?.frame.size.width)!)
            self.titleEdgeInsets = UIEdgeInsets(top: 0, left: -(image?.size.width)!, bottom: 0, right: (image?.size.width)!)
        case .topImage:
            self.imageEdgeInsets = UIEdgeInsets(top:-(self.titleLabel?.frame.size.height)!, left: 0, bottom: 0, right:-((self.titleLabel?.frame.size.width)!))
            //圖片距離右邊框距離減少圖片的寬度笆檀,距離上m邊距的距離減少文字的高度
            self.titleEdgeInsets = UIEdgeInsets(top: ((image?.size.height)!), left: -((image?.size.width)!), bottom: 0, right:0)
        //文字距離上邊框的距離增加imageView的高度忌堂,距離左邊框減少imageView的寬度,距離下邊框和右邊框距離不變
        default:
            self.imageEdgeInsets = UIEdgeInsets(top: (self.titleLabel?.frame.size.height)!, left:0, bottom: 0, right:-((self.titleLabel?.frame.size.width)!))
            //圖片距離上邊距增加文字的高度  距離右邊距減少文字的寬度
            self.titleEdgeInsets = UIEdgeInsets(top: -(image?.size.height)!, left: -(image?.size.width)!, bottom: 0, right: 0)
        }
    }
}
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末酗洒,一起剝皮案震驚了整個(gè)濱河市士修,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌樱衷,老刑警劉巖棋嘲,帶你破解...
    沈念sama閱讀 218,607評(píng)論 6 507
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場(chǎng)離奇詭異矩桂,居然都是意外死亡沸移,警方通過(guò)查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,239評(píng)論 3 395
  • 文/潘曉璐 我一進(jìn)店門(mén),熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)雹锣,“玉大人网沾,你說(shuō)我怎么就攤上這事∪锞簦” “怎么了辉哥?”我有些...
    開(kāi)封第一講書(shū)人閱讀 164,960評(píng)論 0 355
  • 文/不壞的土叔 我叫張陵,是天一觀(guān)的道長(zhǎng)攒射。 經(jīng)常有香客問(wèn)我醋旦,道長(zhǎng),這世上最難降的妖魔是什么会放? 我笑而不...
    開(kāi)封第一講書(shū)人閱讀 58,750評(píng)論 1 294
  • 正文 為了忘掉前任饲齐,我火速辦了婚禮,結(jié)果婚禮上咧最,老公的妹妹穿的比我還像新娘捂人。我一直安慰自己,他們只是感情好矢沿,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,764評(píng)論 6 392
  • 文/花漫 我一把揭開(kāi)白布先慷。 她就那樣靜靜地躺著,像睡著了一般咨察。 火紅的嫁衣襯著肌膚如雪论熙。 梳的紋絲不亂的頭發(fā)上,一...
    開(kāi)封第一講書(shū)人閱讀 51,604評(píng)論 1 305
  • 那天摄狱,我揣著相機(jī)與錄音脓诡,去河邊找鬼。 笑死媒役,一個(gè)胖子當(dāng)著我的面吹牛祝谚,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播酣衷,決...
    沈念sama閱讀 40,347評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼交惯,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼!你這毒婦竟也來(lái)了穿仪?” 一聲冷哼從身側(cè)響起席爽,我...
    開(kāi)封第一講書(shū)人閱讀 39,253評(píng)論 0 276
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎啊片,沒(méi)想到半個(gè)月后只锻,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,702評(píng)論 1 315
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡紫谷,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,893評(píng)論 3 336
  • 正文 我和宋清朗相戀三年齐饮,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了捐寥。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 40,015評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡祖驱,死狀恐怖握恳,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情捺僻,我是刑警寧澤睡互,帶...
    沈念sama閱讀 35,734評(píng)論 5 346
  • 正文 年R本政府宣布,位于F島的核電站陵像,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏寇壳。R本人自食惡果不足惜醒颖,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,352評(píng)論 3 330
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望壳炎。 院中可真熱鬧泞歉,春花似錦、人聲如沸匿辩。這莊子的主人今日做“春日...
    開(kāi)封第一講書(shū)人閱讀 31,934評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)铲球。三九已至挺庞,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間稼病,已是汗流浹背选侨。 一陣腳步聲響...
    開(kāi)封第一講書(shū)人閱讀 33,052評(píng)論 1 270
  • 我被黑心中介騙來(lái)泰國(guó)打工, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留然走,地道東北人援制。 一個(gè)月前我還...
    沈念sama閱讀 48,216評(píng)論 3 371
  • 正文 我出身青樓,卻偏偏與公主長(zhǎng)得像芍瑞,于是被迫代替她去往敵國(guó)和親晨仑。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,969評(píng)論 2 355