49-Swift 之UIBarButtonItem的使用和說明

一 、UIBarButtonItem 介紹

UIBarButtonItem 是一個繼承與 UIBarItem 的一個類,常用語導(dǎo)航欄的左右按鈕。

二 、 UIBarButtonItem 的方法說明

1、初始化的方法

》》 init 創(chuàng)建一個對象
    public init()

》》init?(coder aDecoder: NSCoder) 內(nèi)存記錄創(chuàng)建一個對象
    public init?(coder aDecoder: NSCoder)

》》生成只是圖片的對象
    public convenience init(image: UIImage?, style: UIBarButtonItemStyle, target: Any?, action: Selector?)

》》生成一個只含有圖片的對象,并且可以在狀態(tài)欄顯示的圖像
    public convenience init(image: UIImage?, landscapeImagePhone: UIImage?, style: UIBarButtonItemStyle, target: Any?, action: Selector?)

》》生成一個只含所有文字的對象
    public convenience init(title: String?, style: UIBarButtonItemStyle, target: Any?, action: Selector?)

》》通過系統(tǒng)的UIBarButtonSystemItem 來創(chuàng)一個對象
    public convenience init(barButtonSystemItem systemItem: UIBarButtonSystemItem, target: Any?, action: Selector?)

》》通過一個View 來創(chuàng)建對象
    public convenience init(customView: UIView)

2、對象的創(chuàng)建和展示

1、只含所有圖像對象
/**
 這種快捷初始化是:只有圖片的按鈕
 */
let BtnItem2 = UIBarButtonItem.init(image: UIImage.init(named: "back")?.withRenderingMode(.alwaysOriginal), style: .plain, target: self, action: #selector(imageBarButtonItemMethod))
self.navigationItem.leftBarButtonItem = BtnItem2

展示圖像

4707D5DF-6CDA-4B27-8D9D-49249BD743FD.png
2、只含有文字的對象
/**
 只有文字的初始化
 */
let BtnItem3 = UIBarButtonItem.init(title: "返回", style: .done, target: self, action: #selector(titleBarButtonItemMethod))
self.navigationItem.leftBarButtonItem = BtnItem3

展示圖像

889483DD-0859-4E6A-9D82-AF475A017E8D.png
3、 使用UIBarButtonSystemItem來創(chuàng)建對象
/**
 顯示系統(tǒng)的 UIBarButtonSystemItem ,創(chuàng)建的UIBarButtonItem
 */
let ItmeS = NSMutableArray.init()
for  i in 0...24 {
    let BtnItem4 = UIBarButtonItem.init(barButtonSystemItem: UIBarButtonSystemItem(rawValue: i)!, target: self, action: #selector(systemItemBarButtonItemMethod))
    ItmeS.add(BtnItem4)
}
self.navigationItem.leftBarButtonItems = (ItmeS as! [UIBarButtonItem])

展示圖片


![B992BAE8-205B-4714-B005-1F6450A2B394.png](http://upload-images.jianshu.io/upload_images/3280268-19f336b41e2455b5.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
4、當(dāng)導(dǎo)航欄緊湊的時候,按鈕圖片將會顯示到狀態(tài)欄里
/**
 landscapeImagePhone將用于工具欄按鈕圖像當(dāng)導(dǎo)航欄緊湊或濃縮欄指標(biāo)。
 */
let BtnItem5 = UIBarButtonItem.init(image: UIImage.init(named: "back")?.withRenderingMode(.alwaysOriginal), landscapeImagePhone: UIImage.init(named: "back")?.withRenderingMode(.alwaysOriginal), style: .done, target: self, action: #selector(landscapeImageBarButtonItemMethod))
self.navigationItem.leftBarButtonItem =BtnItem5
5、這些按鈕還可以一起顯示在導(dǎo)航上
let BtnItem2 = UIBarButtonItem.init(image: UIImage.init(named: "back")?.withRenderingMode(.alwaysOriginal), style: .plain, target: self, action: #selector(imageBarButtonItemMethod))
let BtnItem3 = UIBarButtonItem.init(title: "返回", style: .done, target: self, action: #selector(titleBarButtonItemMethod))
let BtnItem4 = UIBarButtonItem.init(barButtonSystemItem: .bookmarks, target: self, action: #selector(systemItemBarButtonItemMethod))
self.navigationItem.leftBarButtonItems = [BtnItem2,BtnItem3,BtnItem4]

展示圖片

9765C96A-A610-49FA-8885-F870674E154D.png

3、屬性的說明和使用

1、設(shè)置UIBarButtonItem按鈕的文字顏色,不影響圖片
BtnItem3.tintColor = UIColor.red

圖像展示

6C32274B-A55D-4CC8-A040-F8CD34B0F2D7.png
2、設(shè)置背景圖片 & 獲取背景圖片

》#設(shè)置圖片

/**
 設(shè)置UIBarButtonItem的背景圖片
 */
BtnItem3.setBackgroundImage(UIImage.init(named: "2"), for: .normal, barMetrics: .default)

圖像展示

1BC0EC3C-6A49-47D9-B892-8F06DBCD0BC9.png

》#獲取圖像

/**
 獲取設(shè)置的背景圖片
 */
let backBtnItemImage = BtnItem3.backgroundImage(for: .normal, barMetrics: .default)
print(backBtnItemImage!)
3、設(shè)置按鈕的文字的顯示位置
/**
 設(shè)置UIBarButtonItem文字在按鈕框內(nèi)的位置
 horizontal >0  文字顯示在水平中心偏右的位置
 horizontal <0  文字顯示在水平中心偏左的位置
 vertical  > 0  文字顯示在垂直中心偏上的位置
 vertical  < 0  文字顯示在垂直中心偏下的位置

 */
BtnItem3.setTitlePositionAdjustment(UIOffset.init(horizontal: 0, vertical: 0), for: .default)
4、調(diào)整按鈕在導(dǎo)航欄的垂直位置
/**
 條件是:self.navigationController?.navigationBar.frame 的高度大于系統(tǒng)默認(rèn)設(shè)置的高度時才有效。
 調(diào)整按鈕的垂直位置
 */
BtnItem2.setBackgroundVerticalPositionAdjustment(-40, for: .default)
let adjustmentValue = BtnItem2.backButtonTitlePositionAdjustment(for: .default)
print(adjustmentValue)

圖像展示

BC6C1F5C-6C7C-4572-8692-1927769CF4B9.png
5 、一個未測試出來的方法

BtnItem3.setBackButtonBackgroundVerticalPositionAdjustment(-140, for: .default)
let VerticalPositionAdjustmentValue = BtnItem3.backButtonBackgroundVerticalPositionAdjustment(for: .default)
print(VerticalPositionAdjustmentValue)

6、全局改變按鈕文字的顏色或者樣式的方法
UIBarButtonItem.appearance().setTitleTextAttributes([NSFontAttributeName:UIFont.boldSystemFont(ofSize: 20),NSForegroundColorAttributeName:UIColor.red], for: .normal)
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容