删除 iOS UIBarButtonItem 的标题文本
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19078995/
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
Removing the title text of an iOS UIBarButtonItem
提问by Pan Ziyue
What I wanted to do is to remove the text from the 'Back' button of a UIBarButtonItem
, leaving only the blue chevron on the navigation bar. Keep in mind that I'm developing for iOS 7. I've tried several methods, including, but not limited to:
我想要做的是从 a 的“后退”按钮中删除文本UIBarButtonItem
,只在导航栏上留下蓝色 V 形标志。请记住,我正在为 iOS 7 开发。我尝试了多种方法,包括但不限于:
This is the image method which I did not like (the image looked out of place):
这是我不喜欢的图像方法(图像看起来不合适):
UIBarButtonItem *barBtnItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"iOS7BackButton"] style:UIBarButtonItemStylePlain target:self action:@selector(goToPrevious:)];
self.navigationItem.leftBarButtonItem = barBtnItem;
Another method I tried was this, which simply did not work (nothing was displayed):
我尝试过的另一种方法是这样,它根本不起作用(没有显示任何内容):
UIBarButtonItem *barBtn = [[UIBarButtonItem alloc]init];
barBtn.title=@"";
self.navigationItem.leftBarButtonItem=barBtn;
What I wanted to achieve is something like the back buttons found in the iOS 7 Music app, which only featured a single chevron.
我想要实现的是类似于 iOS 7 音乐应用程序中的后退按钮,它只有一个 V 形标志。
Thanks.
谢谢。
采纳答案by andyleehao
[[UIBarButtonItem appearance] setBackButtonTitlePositionAdjustment:UIOffsetMake(-60, -60)
forBarMetrics:UIBarMetricsDefault];
Then you can remove the back button item title.
然后您可以删除后退按钮项目标题。
If you use Storyboard, you can set navigation attributes inspector Back Button with space.
如果您使用 Storyboard,您可以设置导航属性检查器 Back Button 与空格。
回答by DonnaLea
To set the back button title for a view controller without changing its title use:
要为视图控制器设置后退按钮标题而不更改其标题,请使用:
Objective-C:
目标-C:
self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"" style:self.navigationItem.backBarButtonItem.style target:nil action:nil];
Swift:
迅速:
navigationItem.backBarButtonItem = UIBarButtonItem(title: "", style: .plain, target: nil, action: nil)
To be clear, this is done on the view controller that you would see if you hit the back button.i.e. instead of seeing '< Settings' you want to just see '<' then on your SettingsViewController you would put this in your init
. Then you don't get any of the problems of the title not showing when you're looking at the view controller itself.
需要明确的是,这是在视图控制器上完成的,如果您点击后退按钮,您会看到它。即而不是看到 '< Settings' 你只想看到 '<' 然后在你的 SettingsViewController 上你会把它放在你的init
. 然后,当您查看视图控制器本身时,您不会遇到标题未显示的任何问题。
回答by Nikita Kukushkin
If you are using Storyboards you can go to Attributes Inspector
of the ViewController's Navigation Item
(click on Navigation Bar
) and set the Back Button
property to " " (one space character). This will set the Back Button title to one space character, leaving the chevron visible. No need to mess with code.
如果您使用的是 Storyboard,您可以转到Attributes Inspector
ViewController 的Navigation Item
(单击Navigation Bar
)并将Back Button
属性设置为“”(一个空格字符)。这会将后退按钮标题设置为一个空格字符,使 V 形标记可见。无需弄乱代码。
Note that this will set Back Button
title for the Back Button that will segue to this View Controllerfrom the one that was pushed on top of it, not for the Back Button
that will be displayed inside this Controller!
请注意,这将为后退按钮设置Back Button
标题,该按钮将从被推到它上面的那个视图控制器转到这个视图控制器,而不是Back Button
将显示在这个控制器中!
回答by Guto Araujo
This works for me to display just the 'back' chevron without any text:
这对我来说只显示没有任何文本的“后”V 字形:
self.navigationController.navigationBar.topItem.title = @"";
Set this property in viewDidLoad
of the View Controller presenting the navigation bar and it will do the trick.
在viewDidLoad
呈现导航栏的视图控制器中设置此属性,它就会起作用。
Note: I have only tested it in iOS 7, which is within scope of the question.
注意:我只在 iOS 7 中测试过它,这在问题的范围内。
回答by Kamaros
When you're setting the button's title, use @" " instead of @"".
当您设置按钮的标题时,请使用 @" " 而不是 @""。
--EDIT--
- 编辑 -
Does anything change when you try other strings? I'm using the following code myself successfully:
当您尝试其他字符串时,有什么变化吗?我自己成功地使用了以下代码:
UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithTitle:backString style:UIBarButtonItemStyleDone target:nil action:nil];
[[self navigationItem] setBackBarButtonItem:backButton];
backString is a variable that is set to @" " or @"Back", depending on if I'm on iOS 7 or a lower version.
backString 是设置为@" " 或@"Back" 的变量,具体取决于我使用的是 iOS 7 还是更低版本。
One thing to note is that this code isn't in the controller for the page I want to customize the back button for. It's actually in the controller before it on the navigation stack.
需要注意的一件事是,此代码不在我要为其自定义后退按钮的页面的控制器中。它实际上在导航堆栈上的控制器中。
回答by Suragch
Sometimes it is helpful to see things in context. Here is a minimal project that hides the "back" text but still shows the arrow.
有时,在上下文中看待事物是有帮助的。这是一个隐藏“后退”文本但仍显示箭头的最小项目。
Storyboard
故事板
There is a show segue from the "Show Second View Controller" button to the second view controller.
从“显示第二个视图控制器”按钮到第二个视图控制器有一个显示转场。
I also added a Navigation Item to the second view controller so that it would have a title. This is optional. It does not affect the back button.
我还在第二个视图控制器中添加了一个导航项,以便它有一个标题。这是可选的。它不会影响后退按钮。
Code
代码
FirstViewController.swift
FirstViewController.swift
import UIKit
class FirstViewController: UIViewController {
@IBAction func showSecondViewControllerButtonTapped(sender: UIButton) {
// hide the back button text
navigationItem.backBarButtonItem = UIBarButtonItem(title: "", style: .plain, target: nil, action: nil)
}
}
SecondViewController.swift
SecondViewController.swift
import UIKit
class SecondViewController: UIViewController {
// Nothing at all needed here
}
Alternate method (IB only, no code)
替代方法(仅限IB,无代码)
On the storyboard select the navigation item for the firstview controller (not the second). Just enter a space for the Back Button text.
在情节提要上选择第一个视图控制器(而不是第二个)的导航项。只需为后退按钮文本输入一个空格。
回答by Claudio Castro
self.navigationController.navigationBar.topItem.title = @"";
回答by DZenBot
On iOS7, Apple introduced two new properties to UINavigationBar, 'backIndicatorTransitionMaskImage' and 'backIndicatorImage'.
在 iOS7 上,Apple 为 UINavigationBar 引入了两个新属性,“backIndicatorTransitionMaskImage”和“backIndicatorImage”。
By simply calling once:
只需调用一次:
[[UINavigationBar appearance] setBackIndicatorImage:[UIImage imageNamed:@"your_image"]];
[[UINavigationBar appearance] setBackIndicatorTransitionMaskImage:[UIImage imageNamed:@"your_image_mask"]];
It will render a custom image instead of the default chevron glyph, inheriting the keyWindow's tint color.
它将呈现自定义图像而不是默认的 V 形字形,继承 keyWindow 的色调颜色。
And for removing the title, I'll suggest Kamaros's answer. Remember to call this code on the view controller that's pushing your new view controller. Removing the title text of an iOS UIBarButtonItem
对于删除标题,我会建议 Kamaros 的回答。请记住在推送新视图控制器的视图控制器上调用此代码。删除 iOS UIBarButtonItem 的标题文本
回答by Grahambo
I didn't have a lot of success with the provided answers but I did find a really simple work around. In your storyboard, you can click on your UIViewController's Navigation Item and set the back button text. I set it to a single ' ' space and it gave me the behavior I was looking for.
我提供的答案并没有取得很大的成功,但我确实找到了一个非常简单的解决方法。在故事板中,您可以单击 UIViewController 的导航项并设置后退按钮文本。我将它设置为单个 ' ' 空间,它给了我我正在寻找的行为。
回答by Ravi
This worked for me in iOS10. Call this from viewDidLoad of the view controller.
这在 iOS10 中对我有用。从视图控制器的 viewDidLoad 调用它。
self.navigationController?.navigationBar.topItem?.title = ""