Swift UIButton使用教程
一.UIButton基本操作
1.創(chuàng)建按鈕
let btn: UIButton = UIButton()//沒有樣式 let btns:UIButton =UIButton(type: UIButtonType)//有樣式 let button = UIButton(frame:CGRect(x:10, y:150, width:100, height:30))//簡化創(chuàng)建方式
UIButtonType有以下類型
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)類型
/**
Normal (默認(rèn)狀態(tài))
Highlighted (高亮狀態(tài))點(diǎn)擊按鈕不放
Disabled (使能狀態(tài))就是是否可用狀態(tài)-->禁用的狀態(tài)才會(huì)顯現(xiàn)
Selected (選中狀態(tài))通過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)原來的顏色 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í)針方向,上、左、下、右依次去設(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í)間有以下類型
touchDown:單點(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)榉派咸嗍种付蝗∠?,或者電話打?/p>
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("你是誰啊,其實(shí)就是一個(gè)按鈕")
}
二.自定義操作
1.UIButton的圖片文字布局
創(chuàng)建一個(gè)按鈕且其同時(shí)擁有文字和圖片屬性,會(huì)按照系統(tǒng)的默認(rèn)樣式(左圖片,右文字)顯示。但是有的時(shí)候,我們會(huì)遇到其他的設(shè)計(jì)需求,比如:左文字有圖片、上文字下圖片、上圖片下文字等。這個(gè)時(shí)候就需要我們自定義按鈕的顯示樣式來滿足復(fù)雜多變的設(shè)計(jì)需求了。 我自定義了一個(gè)按鈕的extension來滿足以上的功能,代碼如下:
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)
}
}
}
以上就是Swift UIButton使用教程的詳細(xì)內(nèi)容,更多關(guān)于Swift UIButton的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
用Swift構(gòu)建一個(gè)簡單的iOS郵件應(yīng)用的方法
這篇文章主要介紹了用Swift構(gòu)建一個(gè)簡單的iOS郵件應(yīng)用的方法,包括查看和標(biāo)記已讀等基本的郵件應(yīng)用功能,需要的朋友可以參考下2015-07-07
Swift之for循環(huán)的基礎(chǔ)使用學(xué)習(xí)
這篇文章主要為大家介紹了Swift之for循環(huán)的基礎(chǔ)學(xué)習(xí),有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-06-06
swift中AnyObject和Any的介紹與區(qū)別詳解
雖然使用swift開發(fā)了一段時(shí)間,但是感覺對(duì)一些基礎(chǔ)的東西了解不是比較透徹,在查詢了許多資料以后還是打算自己動(dòng)手記錄一下,下面這篇文章主要給大家介紹了關(guān)于swift中AnyObject和Any的介紹與區(qū)別的相關(guān)資料,需要的朋友可以參考下。2017-12-12
詳談swift內(nèi)存管理中的引用計(jì)數(shù)
下面小編就為大家?guī)硪黄斦剆wift內(nèi)存管理中的引用計(jì)數(shù)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-09-09
Swift5中fileprivate與private的差別淺析
這篇文章主要給大家介紹了關(guān)于Swift5中fileprivate與private的差別的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用Swift5具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2019-09-09
swiftui開發(fā)之padding默認(rèn)值設(shè)置詳解
這篇文章主要為大家介紹了swiftui開發(fā)之padding默認(rèn)值設(shè)置詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-09-09

