2016-11-09 70 views
0

我想將快捷鍵中的後退按鈕改爲任何名字3.我嘗試了很多方法,但都沒有爲我工作。如何在swift3中更改導航控制器上的返回按鈕標題?

self.navigationItem.backBarButtonItem?.title="Title" 
self.navigationController?.navigationItem.backBarButtonItem?.title="Title" 

僅供參考,我在下面的代碼中寫了appdelegate。

let backImage : UIImage = UIImage(named:"backArrow")! 
UINavigationBar.appearance().barTintColor = UIColor(red: 0.0/255.0, green: 0.0/255.0, blue: 0.0/255.0, alpha: 1.0) 
UINavigationBar.appearance().tintColor = UIColor.white 
UINavigationBar.appearance().backIndicatorImage = backImage 
UINavigationBar.appearance().backIndicatorTransitionMaskImage = backImage 
UINavigationBar.appearance().titleTextAttributes = [NSForegroundColorAttributeName : UIColor.white] 
UIBarButtonItem.appearance().setBackButtonTitlePositionAdjustment(UIOffsetMake(0, 0), for: .default) 
IQKeyboardManager.sharedManager().enable = true 
self.window?.backgroundColor = UIColor.white 
+0

檢查此http: //stackoverflow.com/a/32284525/4003548。 – vaibhav

+1

的可能的複製[如何設置回斯威夫特按鈕文本(http://stackoverflow.com/questions/28471164/how-to-set-back-button-text-in-swift) –

+0

[請reffer可能會回答你問題](http://stackoverflow.com/questions/1449339/how-do-i-change-the-title-of-the-back-button-on-a-navigation-bar) –

回答

4

導航項返回按鈕名稱將是相同前視圖控制器的其它推到導航堆棧:)

因此,如果VC甲推VC B,在VC乙後退按鈕的標題將是A.

因此,所有你能做的就是,使用代碼:)

self.navigationItem.title = "ABCD" 

而在六推新的viewController之前,改變了以前的viewController的標題VC A的ewWillAppear,你能恢復的標題回到不管它是較早:)

self.navigationItem.title = "Back to xyz" 

所有這一切是說,如果你不希望這一切馬戲團:)你可以使用簡單的隱藏默認後退按鈕,

self.navigationItem.hidesBackButton = true 
在VC乙

,創建一個UIBarButton項,設置要設置任何標題,然後設置爲leftBarButtonItem :)使用,

self.navigationItem.leftBarButtonItem = UIBarButtonItem(title: NSLocalizedString("ABCD", comment: "ABCD"), style: .plain, target: self, action:#selector(self.abcdTapped:) 
當然

現在將不顯示「<「圖標:)現在,如果你想要的,以及你可以將其添加爲圖像回欄按鈕項目:)但其繁瑣:)

希望它能幫助:)

2

嘗試以下步驟設置圖像到你的後退按鈕..

輸出:

enter image description here

第1步:

添加以下代碼到你的的AppDelegate

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { 
    // Override point for customization after application launch. 

    var backButtonImage = UIImage(named: "Your Image Name") 
    backButtonImage = backButtonImage?.stretchableImage(withLeftCapWidth: 0, topCapHeight: 0) 
    UIBarButtonItem.appearance().setBackButtonBackgroundImage(backButtonImage, for: .normal, barMetrics: .default) 
    UINavigationBar.appearance().barTintColor = UIColor(red: 0.0/255.0, green: 0.0/255.0, blue: 0.0/255.0, alpha: 1.0) 
    UINavigationBar.appearance().tintColor = UIColor.white 

    return true 
} 

第2步:

添加以下代碼到你的MainVC

override func viewDidLoad() { 
    super.viewDidLoad() 
    // Do any additional setup after loading the view, typically from a nib. 

    title = "Title 1" 
    navigationController?.navigationBar.titleTextAttributes = [NSForegroundColorAttributeName:UIColor.white, NSFontAttributeName:UIFont(name:"HelveticaNeue", size: 20)!] 
    } 

第3步:

添加以下代碼到你的DestVC或2ndVC

override func viewDidLoad() { 
    super.viewDidLoad() 
    // Do any additional setup after loading the view, typically from a nib. 

    title = "Title 2" 
    navigationController?.navigationBar.titleTextAttributes = [NSForegroundColorAttributeName:UIColor.white, NSFontAttributeName:UIFont(name:"HelveticaNeue", size: 20)!] 
    } 

第4步:

故事板和GOTO 屬性檢查器選擇您的導航欄。在導航項目改變你的背部按鈕名稱進入一個空的空間或編程方式創建與普通的標題後退按鈕..

enter image description here

5步:

添加圖標圖像您的資產。 1x29pt2x58pt3x87pt。我不知道有關資產圖像size.Check與蘋果文檔大小約類..

enter image description here


更新:

我與此相關的帖子類似的答案。

How to customize the navigation back symbol and navigation back text?

4

你必須設置你推說viewControllerviewControllernavigationItembackBarButtonItem財產。

self.navigationItem.backBarButtonItem = UIBarButtonItem(title: "Back", style: UIBarButtonItemStyle.plain, target: nil, action: nil) 

但是,您必須爲每個viewController設置此值。

3

In Swift 3。0把下面的代碼在appdelegatedidFinishLaunchingWithOptions方法其完美的工作對我來說

let backImage = UIImage(named: "BackNavigation")?.withRenderingMode(.alwaysOriginal) 
UINavigationBar.appearance().backIndicatorImage = backImage 
UINavigationBar.appearance().backIndicatorTransitionMaskImage = backImage 
UIBarButtonItem.appearance().setBackButtonTitlePositionAdjustment(UIOffsetMake(0, -80.0), for: .default) 

最後一行將刪除的Navigation Back Button標題,如果你不想刪除標題,然後只是評論它

+3

不再適用於iOS 11 –

相關問題