How do i remove text from back button on navbar within Xcode?

后端 未结 8 1816
醉酒成梦
醉酒成梦 2021-02-09 05:49

I set an arrow custom image to navigation bar by adding the following code in app delegate, it works but now im looking to remove the text completely for back button.

         


        
相关标签:
8条回答
  • 2021-02-09 06:02

    To set a backbutton text, you set a new backbutton to the current viewController before pushing or presenting then new one which would show the text of the backbutton:

    In your current viewController (not the new one which will show the back-button):

    vc                                    = [[MyNewViewController alloc]initWith...];
    vc.title                              = @"New ViewController";
    self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"Back" style:UIBarButtonItemStylePlain target:nil action:nil];
    [self.navigationController pushViewController:vc animated:YES];
    

    So if you want to remove the text, just use @"" as a title for the new backbutton.

    To set a backbutton-icon for the entire app, use the following code in your appDelegate class. Not every icon fits perfectly, so if you have to move it around a little, you can use the "backInsets". In my example the icon will move 2px down.

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
        ...
        ...
        [self customBackButtonIcon];
        return YES;
    }
    
    - (void)customBackButtonIcon
    {        
        UIEdgeInsets backInsets = UIEdgeInsetsMake(0, 0, -2, 0);
        UIImage *backImg = [[[UIImage imageNamed:@"back_button_white"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] imageWithAlignmentRectInsets:backInsets];
        [[UINavigationBar appearance] setBackIndicatorImage:backImg];
        [[UINavigationBar appearance] setBackIndicatorTransitionMaskImage:backImg];
    }
    

    Tested with iOS9

    0 讨论(0)
  • 2021-02-09 06:04

    I just did it like this, worked fine for me :-)

    UIImage *backButtonImage = [UIImage imageNamed:@"navigationBarBack.png"];
    UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
    
    [backButton setImage:backButtonImage
                forState:UIControlStateNormal];
    
    backButton.frame = CGRectMake(0, 0, backButtonImage.size.width, backButtonImage.size.height);
    
    [backButton addTarget:self
                   action:@selector(popViewController)
         forControlEvents:UIControlEventTouchUpInside];
    
    UIBarButtonItem *backBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];
    self.navigationItem.leftBarButtonItem = backBarButtonItem;
    
    0 讨论(0)
  • 2021-02-09 06:04

    Do not use the appearance proxy. Instead, for every view controller, put this code into its viewDidLoad implementation:

    UIImage * backButtonImage = 
        [UIImage imageNamed: @"BackButtonGrey.png"];
    backButtonImage = 
        [backButtonImage stretchableImageWithLeftCapWidth: 15.0 topCapHeight: 30.0];
    
    UIBarButtonItem* b =
         [[UIBarButtonItem alloc] 
              initWithImage:backButtonImage
              style:UIBarButtonItemStylePlain target:nil action:nil];
    self.navigationItem.backBarButtonItem = b;
    

    That will cause the next view controller pushed onto the navigation stack to have a back button consisting of just the image.

    (However, I should point out that stretchableImageWithLeftCapWidth:... is deprecated. You should be using resizableImage....)

    0 讨论(0)
  • 2021-02-09 06:05

    I really don't think is a good practice for a developer to adjust the offset of the text in order to hide it.

    A cleaner solution would be to just add to the navigation controller back button a new button where the title is an empty string. You should add this in the previous calling view controller in viewWillAppear (not the current one):

    UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithTitle:@"" style:UIBarButtonItemStylePlain target:nil action:nil];
    self.navigationItem.backBarButtonItem = backButton;
    
    0 讨论(0)
  • 2021-02-09 06:07
    [[UIBarButtonItem appearance] setBackButtonTitlePositionAdjustment:UIOffsetMake(-100.f, 0) forBarMetrics:UIBarMetricsDefault];
    
    0 讨论(0)
  • 2021-02-09 06:13
    UIBarButtonItem *newBackButton =
    [[UIBarButtonItem alloc] initWithTitle:@""
                                     style:UIBarButtonItemStylePlain
                                    target:nil
                                    action:nil];
    [[self navigationItem] setBackBarButtonItem:newBackButton];
    
    0 讨论(0)
提交回复
热议问题