2016-11-09 24 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 –

相关问题