ios開發Swift修改導覽列“返回”按鈕文字,表徵圖

來源:互聯網
上載者:User

項目中常常會使用 UINavigationController 對各個頁面進行導航,導覽列左側的返回按鈕預設標題文字是上級頁面的title。

但如果上級頁面的標題很長,那麼這個返回按鈕字很多就會很醜:
      

 

當文字極其長時返迴文字就會變成“back”: 
      


一,要修改“返回按鈕”的文字,有如下兩種方式:

1,在父頁面中設定
將navigationItem.backBarButtonItem設為自訂的UIBarButtonItem
這種方法所有的子介面返回時都變成了我們定義的文字,同時文字前面任然保留返回箭頭。



let item = UIBarButtonItem(title: "返回", style: .Plain, target: self, action: nil)
self.navigationItem.backBarButtonItem = item;

 

或者也可以直接把文字設為空白字串,這樣就只有一個箭頭了。

 


let item = UIBarButtonItem(title: "", style: .Plain, target: self, action: nil)
self.navigationItem.backBarButtonItem = item;

 

2,在子頁面中設定
將navigationItem.leftBarButtonItem為自訂的UIBarButtonItem
這種方式可以給各個子頁面返回按鈕單獨設定不同的文字,但文字前面是沒有小箭頭的。

 


import UIKit
 
class DetailViewController: UIViewController {
 
    override func viewDidLoad() {
        super.viewDidLoad()
         
        let leftBarBtn = UIBarButtonItem(title: "返回", style: .Plain, target: self,
            action: "backToPrevious")
        self.navigationItem.leftBarButtonItem = leftBarBtn
    }
     
    //返回按鈕點擊響應
    func backToPrevious(){
        self.navigationController?.popViewControllerAnimated(true)
    }
 
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }
}

二,修改“返回按鈕”表徵圖 

從上面最後一個例子可以看到,在子頁面修改返回按鈕的話只有文字沒有圖片。如果想要使用自訂圖片,或者圖片文字都需要的話可以進行如下操作:

1,如果只需要圖片,不需要文字

比如我們想要用左側這個圖片(back@2x.png)作為返回表徵圖

               

import UIKit
 
class DetailViewController: UIViewController {
 
    override func viewDidLoad() {
        super.viewDidLoad()
         
        let leftBarBtn = UIBarButtonItem(title: "", style: .Plain, target: self,
            action: "backToPrevious")
        leftBarBtn.image = UIImage(named: "back")
         
        //用於消除左邊空隙,要不然按鈕頂不到最前面
        let spacer = UIBarButtonItem(barButtonSystemItem: .FixedSpace, target: nil, action: nil)
        spacer.width = -10;
         
        self.navigationItem.leftBarButtonItems = [spacer, leftBarBtn]
    }
     
    //返回按鈕點擊響應
    func backToPrevious(){
        self.navigationController?.popViewControllerAnimated(true)
    }
 
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }
}

2,既需要圖片也需要文字


這個時候就要通過建立UIButton來實現了

import UIKit
 
class DetailViewController: UIViewController {
     
    override func viewDidLoad() {
         
        let button =   UIButton(type: .System)
        button.frame = CGRectMake(0, 0, 65, 30)
        button.setImage(UIImage(named:"back"), forState: .Normal)
        button.setTitle("返回", forState: .Normal)
        button.addTarget(self, action: "backToPrevious", forControlEvents: .TouchUpInside)
         
        let leftBarBtn = UIBarButtonItem(customView: button)
         
        //用於消除左邊空隙,要不然按鈕頂不到最前面
        let spacer = UIBarButtonItem(barButtonSystemItem: .FixedSpace, target: nil, action: nil)
        spacer.width = -10;
         
        self.navigationItem.leftBarButtonItems = [spacer,leftBarBtn]
    }
     
    //返回按鈕點擊響應
    func backToPrevious(){
        self.navigationController?.popViewControllerAnimated(true)
    }
     
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }
}

相關文章

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.