Removing the title text of an iOS UIBarButtonItem

2019-01-04 15:38发布

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:

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.

Thanks.

30条回答
啃猪蹄的小仙女
2楼-- · 2019-01-04 16:05

Actually you can do this with just one trick:

Override UINavigationBar class and add this line of code:

- (void)layoutSubviews{
    self.backItem.title = @"";
    [super layoutSubviews];
}

Then initialize your UINavigationController with this custom UINavigationBar class.. etc. UINavigationController * navController = [[UINavigationController alloc] initWithNavigationBarClass:[CBCNavigationBar class] toolbarClass:nil];

Hope this helps

查看更多
淡お忘
3楼-- · 2019-01-04 16:05

Swift 3.1 You can do this by implementing the delegate method of UINavigationController. It'll hide the Title with back button only, we'll still get the back arrow image and default functionality.

func navigationController(_ navigationController: UINavigationController, 
  willShow viewController: UIViewController, animated: Bool) {
        let item = UIBarButtonItem(title: " ", style: .plain, target: nil, 
                    action: nil)
        viewController.navigationItem.backBarButtonItem = item
    }
查看更多
萌系小妹纸
4楼-- · 2019-01-04 16:12

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.

example image

Note that this will set Back Button title for the Back Button that will segue to this View Controller from the one that was pushed on top of it, not for the Back Button that will be displayed inside this Controller!

查看更多
倾城 Initia
5楼-- · 2019-01-04 16:13

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.

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.

查看更多
贼婆χ
6楼-- · 2019-01-04 16:13

This is using subclass navigationController removes the "Back".

I'm using this to remove it, permanently through the app.

//.h
@interface OPCustomNavigationController : UINavigationController 

@end

//.m
@implementation OPCustomNavigationController

- (void)awakeFromNib
{
    [self backButtonUIOverride:YES];
}

- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated
{
    [self backButtonUIOverride:NO];

    [super pushViewController:viewController animated:animated];
}

- (void)backButtonUIOverride:(BOOL)isRoot
{
    if (!self.viewControllers.count)
        return;

    UIViewController *viewController;

    if (isRoot)
    {
        viewController = self.viewControllers.firstObject;
    }
    else
    {
        int previousIndex = self.viewControllers.count - 1;

        viewController = [self.viewControllers objectAtIndex:previousIndex];
    }

    viewController.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@""
                                                                                       style:UIBarButtonItemStylePlain
                                                                                      target:nil
                                                                                      action:nil];
}

@end
查看更多
▲ chillily
7楼-- · 2019-01-04 16:14

I was able to cobble something together using DonnaLea's answer. This is how the solution appears in my UIViewController subclass:

var backItemTitle:String?

override func viewDidLoad() {
    super.viewDidLoad()

    //store the original title
    backItemTitle = self.navigationController?.navigationBar.topItem?.title

    //remove the title for the back button
    navigationController?.navigationBar.topItem?.title = ""
}

override func willMoveToParentViewController(parent: UIViewController?) {
    super.willMoveToParentViewController(parent)
    if parent == nil {

        //restore the orignal title
        navigationController?.navigationBar.backItem?.title = backItemTitle
    }
}

The problem with the original answer is that it removes the title from the controller when you pop back to it. Attempting to reset the title in viewWillDisappear is too late in the transition process; It causes the title to snap back in instead of animating nicely. However the willMoveToParentViewController happens sooner and allows for the correct behavior.

Caveat: I've only tested this with a normal UINavigationController push / pop. There might be additional unexpected behavior in other situations.

查看更多
登录 后发表回答