ios 如何删除所有导航栏后退按钮标题
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29912489/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to remove all navigationbar back button title
提问by jansma
When I push a UIViewController, it has some title in back button at new UIViewController, if the title has a lot of text, It does not look good in iPhone 4s So I want to remove it.
当我按下 a 时UIViewController,它在 new 的后退按钮中有一些标题UIViewController,如果标题有很多文字,它在 iPhone 4s 中看起来不太好所以我想删除它。
If I add some code in prepareForSeguefunction, it is going to be a trouble.
如果我在prepareForSegue函数中添加一些代码,那就麻烦了。
Any better way to achieve this?
有没有更好的方法来实现这一目标?
回答by Nimit Parekh
If you want back arrow so following code put into AppDelegatefile into didFinishLaunchingWithOptionsmethod.
如果您想要后退箭头,则将以下代码放入AppDelegate文件中的didFinishLaunchingWithOptions方法。
For Objective-C
For Objective-C
[[UIBarButtonItem appearance] setBackButtonTitlePositionAdjustment:UIOffsetMake(0, -60) forBarMetrics:UIBarMetricsDefault];
For Swift
For Swift
let BarButtonItemAppearance = UIBarButtonItem.appearance()
BarButtonItemAppearance.setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.clear], for: .normal)
Another option give below.
下面给出另一种选择。
In Objective C
在 Objective C
self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"" style:UIBarButtonItemStylePlain target:nil action:nil];
In Swift
在 Swift
self.navigationItem.backBarButtonItem = UIBarButtonItem(title:"", style:.plain, target:nil, action:nil)
UPDATE :
更新 :
let BarButtonItemAppearance = UIBarButtonItem.appearance()
let attributes: [NSAttributedStringKey: Any] = [
BarButtonItemAppearance.setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.clear], for: .normal)
NSAttributedStringKey.font: UIFont.systemFont(ofSize: 0.1),
NSAttributedStringKey.foregroundColor: UIColor.clear]
BarButtonItemAppearance.setTitleTextAttributes(attributes, for: .normal)
BarButtonItemAppearance.setTitleTextAttributes(attributes, for: .highlighted)
UPDATE SWIFT 4.1 :
更新 SWIFT 4.1:
let attributes = [NSAttributedStringKey.font: UIFont(name: "Helvetica-Bold", size: 0.1)!, NSAttributedStringKey.foregroundColor: UIColor.clear]
BarButtonItemAppearance.setTitleTextAttributes(attributes, for: .normal)
BarButtonItemAppearance.setTitleTextAttributes(attributes, for: .highlighted)
Using Offset
使用偏移
UIBarButtonItem.appearance().setBackButtonTitlePositionAdjustment(UIOffsetMake(-1000, 0), for:UIBarMetrics.default)
So may be your problem has been solve.
所以可能你的问题已经解决了。
Happy coding.
快乐编码。
回答by Mahesh Babu
Work's like charm on Swift 3
在 Swift 3 上工作就像魅力一样
self.navigationController?.navigationBar.topItem?.title = " "
回答by Karthickkck
I'm using this line of code in AppDelegatefile into didFinishLaunchingWithOptionsmethod to remove the backbutton title.
我在AppDelegatefile intodidFinishLaunchingWithOptions方法中使用这行代码来删除 backbutton title。
Swift 2.x
斯威夫特 2.x
let barAppearace = UIBarButtonItem.appearance()
barAppearace.setBackButtonTitlePositionAdjustment(UIOffsetMake(0, -60), forBarMetrics:UIBarMetrics.Default)
Swift 3.x
斯威夫特 3.x
UIBarButtonItem.appearance().setBackButtonTitlePositionAdjustment(UIOffsetMake(0, -60), for:UIBarMetrics.default)
Swift 4.x
斯威夫特 4.x
UIBarButtonItem.appearance().setTitleTextAttributes([NSAttributedStringKey.foregroundColor: UIColor.clear], for: .normal)
UIBarButtonItem.appearance().setTitleTextAttributes([NSAttributedStringKey.foregroundColor: UIColor.clear], for: UIControlState.highlighted)
回答by Pravin Kamble
Just need go to your Parent ViewControllerfrom where your other ViewControllers are dependent.
只需要从其他 ViewController 依赖的地方转到您的父 ViewController。
override func viewWillDisappear(animated: Bool) {
super.viewWillDisappear(true)
navigationItem.backBarButtonItem = UIBarButtonItem(title: "", style: .Plain, target: nil, action: nil)}
回答by Vivek
Just copy this code in didFinishLaunchingWithOptions launchOptions
只需复制此代码 didFinishLaunchingWithOptions launchOptions
Swift 5
斯威夫特 5
UIBarButtonItem.appearance().setBackButtonTitlePositionAdjustment(UIOffset(horizontal: -1000.0, vertical: 0.0), for: .default)
Swift 4
斯威夫特 4
UIBarButtonItem.appearance().setBackButtonTitlePositionAdjustment(UIOffsetMake(-1000.0, 0.0), for: .default)
回答by oscar castellon
回答by Anit Kumar
You can use xcode 8 and swift 3.0
您可以使用 xcode 8 和 swift 3.0
self.navigationController?.navigationBar.backItem?.title = " "
回答by Kiran jadhav
let barAppearace = UIBarButtonItem.appearance()
barAppearace.setBackButtonTitlePositionAdjustment(UIOffsetMake(0, -60), for:UIBarMetrics.default)
used this line of code in swift 3.0
在 swift 3.0 中使用了这行代码
回答by Connor
You could create a subclass for all UIViewControllers you want this behavior for, and in the subclass's viewDidLoad:
您可以为所有UIViewController想要这种行为的 s创建一个子类,并在子类的 s 中viewDidLoad:
override func viewDidLoad() {
super.viewDidLoad()
self.navigationItem.backBarButtonItem = UIBarButtonItem(
title: "", style: .plain, target: nil, action: nil)
}
This way, you can choose which controllers you want the behavior for, without duplicating code. I prefer my controllers to just say "Back", rather than the title of the previous controller, so I set that title here.
通过这种方式,您可以选择想要行为的控制器,而无需重复代码。我更喜欢我的控制器只说“返回”,而不是前一个控制器的标题,所以我在这里设置了这个标题。
回答by Harry Bloom
Taking inspiration from rordulu'sanswer here, I ended up creating a custom UINavigationController and UINavigation bar which seems to handle all cases of this tricky problem.
从rordulu'sanswer here 中获得灵感,我最终创建了一个自定义 UINavigationController 和 UINavigation 栏,它似乎可以处理这个棘手问题的所有情况。
1) Initialise new UINavigationControllerwith your custom UINavigationBar:
1)UINavigationController用您的自定义初始化 new UINavigationBar:
class CustomNavigationController: UINavigationController {
convenience init() {
self.init(navigationBarClass: CustomNavigationBar.self, toolbarClass: nil)
}
}
2) Set the backItem.titleproperty of the navigation bar to an empty string, every time the view lays itself out
2) 将backItem.title导航栏的属性设置为空字符串,每次视图布局时
class CustomNavigationBar: UINavigationBar {
override func layoutSubviews() {
backItem?.title = ""
super.layoutSubviews()
}
}
Now every time you use this navigation controller and bar combination, it will never have back button text!
现在每次使用这个导航控制器和栏组合时,它永远不会有后退按钮文本!
Note: this should work fine if using storyboards also, just ensure to drop the custom navigation bar component into the view
注意:如果也使用故事板,这应该可以正常工作,只需确保将自定义导航栏组件放入视图中


