Changing the UIBackButtonItem title

前端 未结 14 820
终归单人心
终归单人心 2020-12-04 17:48

I have a navigationController-based app. I want to change the title of the back button for the root view controller. I have tried the following code in the rootViewControlle

相关标签:
14条回答
  • 2020-12-04 18:48

    Maybe you don't wan't every pushed viewController from a viewController have a Cancel button than just set a leftBarButtonItem in the display viewController with a custom title

    self.navigationItem.leftBarButtonItem = [[[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"back", @"") style:UIBarButtonSystemItemCancel target:self action:@selector(popViewController)] autorelease];
    
    - (void)popViewController {
        [self.navigationController popViewControllerAnimated:YES];
    }
    
    0 讨论(0)
  • 2020-12-04 18:50

    This is the best solution for this, don't set the navigationitem title as usual as self.navigationitem.title. Put this code on the parent view didload

        CGRect frame = CGRectMake(0, 0, 320, 44);
    UILabel *label = [[[UILabel alloc] initWithFrame:frame] autorelease];
    label.backgroundColor = [UIColor clearColor];
    label.font = [UIFont boldSystemFontOfSize:20.0];
    label.shadowColor = [UIColor colorWithWhite:0.0 alpha:0.5];
    label.textAlignment = UITextAlignmentCenter;
    label.textColor = [UIColor whiteColor];
    
    self.navigationItem.titleView = label;
    label.text = @"My Navigation Bar";
    

    By setting the title as above the actual navigation title will be nil, so the backbarbutton title will automatically be "Back"

    0 讨论(0)
提交回复
热议问题