ios UINavigationBar 隐藏返回按钮文本
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23853617/
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
UINavigationBar Hide back Button Text
提问by Nils
How can I hide the Back Button Text from an UINavigation Controller? I will only have the "<" and not "< Back"
如何从 UINavigation 控制器隐藏后退按钮文本?我只会有“<”而不是“< Back”
回答by rebello95
In the interface builder, you can select the navigation item of the previous controller and change the Back Button
string to what you'd like the back button to appear as. If you want it blank, for example, just put a space.
在界面生成器中,您可以选择前一个控制器的导航项并将Back Button
字符串更改为您希望后退按钮显示的内容。例如,如果您希望它为空白,只需输入一个空格。
You can also change itwith this line of code:
您还可以使用以下代码行更改它:
[self.navigationItem.backBarButtonItem setTitle:@"Title here"];
[self.navigationItem.backBarButtonItem setTitle:@"Title here"];
Or in Swift:
或者在Swift 中:
self.navigationItem.backBarButtonItem?.title = ""
self.navigationItem.backBarButtonItem?.title = ""
回答by Shahzin KS
You could also do this through storyboard. In the attribute inspector of the navigation item of the previous controlleryou could set " " in the Back button field. Refer Image below. Replace "Your Title here" to " ". By doing this you will achieve the desired result. You don't need to mess with the 'Title' anymore.
你也可以通过故事板来做到这一点。在前一个控制器导航项的属性检查器中,您可以在后退按钮字段中设置“”。参考下图。将“您在此处的标题”替换为“ ”。通过这样做,您将获得所需的结果。你不需要再弄乱“标题”了。
Programmatically you could use
以编程方式您可以使用
[self.navigationItem.backBarButtonItem setTitle:@" "];
[self.navigationItem.backBarButtonItem setTitle:@" "];
where selfrefers to the controller which pushes your desired View controller.
其中self指的是推送所需视图控制器的控制器。
Sample Before, After Navigation bar
导航栏之前、之后的示例
Before
前
After
后
回答by VoidLess
You can implement UINavigationControllerDelegate
like this:
你可以这样实现UINavigationControllerDelegate
:
Older Swift
年长的斯威夫特
func navigationController(navigationController: UINavigationController, willShowViewController viewController: UIViewController, animated: Bool) {
let item = UIBarButtonItem(title: " ", style: .Plain, target: nil, action: nil)
viewController.navigationItem.backBarButtonItem = item
}
Swift 4.x
斯威夫特 4.x
class MyNavigationController: UINavigationController, UINavigationControllerDelegate {
override func viewDidLoad() {
super.viewDidLoad()
self.delegate = self
}
func navigationController(_ navigationController: UINavigationController, willShow viewController: UIViewController, animated: Bool) {
let item = UIBarButtonItem(title: " ", style: .plain, target: nil, action: nil)
viewController.navigationItem.backBarButtonItem = item
}
}
backBarButtonItem
is nil
by default and it affects next pushed controller, so you just set it for all controllers
backBarButtonItem
是nil
在默认情况下它会影响下一个推控制器,所以你只要将它设置为所有控制器
回答by Hymanal
Setting title of the back button to @""
or nil
won't work. You need to set the entire button empty (without a title or image):
将后退按钮的标题设置为@""
或nil
不起作用。您需要将整个按钮设置为空(没有标题或图像):
Objective-C
目标-C
[self.navigationItem setBackBarButtonItem:[[UIBarButtonItem alloc] initWithTitle:@"" style:UIBarButtonItemStylePlain target:nil action:nil]];
Swift
迅速
self.navigationItem.backBarButtonItem = UIBarButtonItem(title: "", style: .plain, target: nil, action: nil)
This should be done on the view controller that's on top of your view controller in navigation stack (i.e. from where you navigate to your VC via pushViewController
method)
这应该在导航堆栈中视图控制器顶部的视图控制器上完成(即从您通过pushViewController
方法导航到 VC 的位置)
回答by alones
Add the following code in viewDidLoad or loadView
在 viewDidLoad 或 loadView 添加以下代码
self.navigationController.navigationBar.topItem.title = @"";
I tested it in iPhone and iPad with iOS 9
我在装有 iOS 9 的 iPhone 和 iPad 上进行了测试
回答by Adam Kaplan
Another solution to this issue for situations where you have a great deal of view controllers is to use a UIAppearance
proxy to effectively hide the back button title text like this:
对于有大量视图控制器的情况,此问题的另一个解决方案是使用UIAppearance
代理来有效地隐藏后退按钮标题文本,如下所示:
UIBarButtonItem *navBarButtonAppearance = [UIBarButtonItem appearanceWhenContainedIn:[UINavigationBar class], nil];
[navBarButtonAppearance setTitleTextAttributes:@{
NSFontAttributeName: [UIFont systemFontOfSize:0.1],
NSForegroundColorAttributeName: [UIColor clearColor] }
forState:UIControlStateNormal];
This solution will render the text as a tiny clear dot, similar to manually setting the back button title to @" "
, except that it affects all nav bar buttons.
此解决方案将文本呈现为一个微小的清晰点,类似于将后退按钮标题手动设置为@" "
,但它会影响所有导航栏按钮。
I don't suggest this as a general solution to the issue because it impacts all navigation bar buttons. It flips the paradigm so that you choose when to showthe button titles, rather than when to hidethe titles.
我不建议将此作为该问题的一般解决方案,因为它会影响所有导航栏按钮。它翻转了范式,以便您选择何时显示按钮标题,而不是何时隐藏标题。
To choose when to show the titles, either manually restore the title text attributes as needed, or create a specialized subclass of UIBarButtonItem
that does the same (potentially with another UIAppearance
proxy).
要选择何时显示标题,请根据需要手动恢复标题文本属性,或者创建一个专门的子类UIBarButtonItem
(可能使用另一个UIAppearance
代理)。
If you have an app where most of the back button titles need to be hidden, and only a few (or none) of the nav buttons are system buttons with titles, this might be for you!
如果您的应用程序需要隐藏大部分后退按钮标题,并且只有少数(或没有)导航按钮是带标题的系统按钮,那么这可能适合您!
(Note: the font size change is needed even though the text color is clear in order to ensure that long titles do not cause the center nav bar title to shift over)
(注意:即使文字颜色清晰,也需要更改字体大小,以确保长标题不会导致中心导航栏标题移动)
回答by teradyl
You can add this Objective-C category to make all "Back" buttons created by a navigation controller have no text. I just added it to my AppDelegate.m file.
您可以添加此 Objective-C 类别,以使导航控制器创建的所有“后退”按钮都没有文本。我刚刚将它添加到我的 AppDelegate.m 文件中。
@implementation UINavigationItem (Customization)
/**
Removes text from all default back buttons so only the arrow or custom image shows up.
*/
-(UIBarButtonItem *)backBarButtonItem
{
return [[UIBarButtonItem alloc] initWithTitle:@"" style:UIBarButtonItemStylePlain target:nil action:nil];
}
@end
PS - (I don't know how to make this extension work with Swift, it was having weird errors. Edits welcome to add a Swift version)
PS - (我不知道如何让这个扩展与 Swift 一起工作,它有奇怪的错误。欢迎编辑添加一个 Swift 版本)
回答by catanore
I tried some above and below but they didn't work. This worked for me:
我在上面和下面尝试了一些,但它们没有用。这对我有用:
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
self.navigationController?.navigationBar.topItem?.title = ""
}
回答by Cezar Signori
The only thing which works with no side-effects is to create a custom back button. As long as you don't provide a custom action, even the slide gesture works.
唯一没有副作用的方法是创建一个自定义后退按钮。只要您不提供自定义操作,即使是滑动手势也能正常工作。
extension UIViewController {
func setupBackButton() {
let customBackButton = UIBarButtonItem(title: " ", style: .plain, target: nil, action: nil)
navigationItem.backBarButtonItem = customBackButton
}}
Unfortunately, if you want all back buttons in the not to have any titles, you need to setup this custom back button in all your view controllers :/
不幸的是,如果您希望所有后退按钮都没有任何标题,则需要在所有视图控制器中设置此自定义后退按钮:/
override func viewDidLoad() {
super.viewDidLoad()
setupBackButton()
}
It is very important you set a whitespace as the title and not the empty string.
将空格设置为标题而不是空字符串非常重要。
回答by Jyothi Babu Pakalz
to remove the Text from backbutton programmatically, used below Code this will work form xcode7 and above.
以编程方式从后退按钮中删除文本,在代码下方使用,这将适用于 xcode7 及更高版本。
self.navigationController.navigationBar.topItem.title = @" ";
self.navigationController.navigationBar.topItem.title = @"";
or
或者
manualLy in storyboards, select the navigation bar on the view controller and put " " in back button text.
手动在故事板中,选择视图控制器上的导航栏并将“”放在后退按钮文本中。
this will work. thanks
这会起作用。谢谢