ios 导航栏上的自定义后退按钮

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8221581/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 15:30:24  来源:igfitidea点击:

Custom back button on navigation bar

iphoneiosuinavigationcontrolleruinavigationitem

提问by SentineL

In my application there are many UIViewControllerswith UINavigationControllers. There must be a "back" button and a "home" UIButtonon the UINavigationBar. All of this works fine.

在我的应用程序中,有很多UIViewControllers带有UINavigationControllers. 必须有一个“后退”按钮和“家”UIButtonUINavigationBar。所有这些工作正常。

But some of my UIViewControllershave long names, and sometimes there is too small place left for it. I'm trying to replace the original label of the "back" button (it shows the title of the previous view) with a custom "Back", but whatever I tried it didn't work:

但是我的一些UIViewControllers名字很长,有时留给它的地方太小了。我试图用自定义的“后退”替换“后退”按钮的原始标签(它显示上一个视图的标题),但无论我尝试过什么,它都不起作用:

// Title didn't change
[self.navigationItem.backBarButtonItem setTitle:@"Back"];

// Action didn't set, no response from button ( button didn't do anything )
[self.navigationItem.leftBarButtonItem
   setAction:self.navigationItem.backBarButtonItem.action];

And I need the "back" button to have a style like in this question: Draw custom Back button on iPhone Navigation Bar

而且我需要“后退”按钮具有类似以下问题的样式:Draw custom Back button on iPhone Navigation Bar

采纳答案by Mihir Mehta

Try this

尝试这个

UIBarButtonItem *backBarBtnItem = [[UIBarButtonItem alloc] initWithTitle:@"Back" style:UIBarButtonItemStylePlain target:self action:@selector(popViewController)];
[self.navigationItem setBackBarButtonItem:backBarBtnItem];

- (void)popViewController
{
    [self.navigationController popViewControllerAnimated:YES];
}

回答by Anil Kothari

Try the following. It will definitely work:

请尝试以下操作。它肯定会起作用:

- (void)viewDidLoad {
    [super viewDidLoad];

    UIImage *buttonImage = [UIImage imageNamed:@"back.png"];
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button setImage:buttonImage forState:UIControlStateNormal];
    button.frame = CGRectMake(0, 0, buttonImage.size.width, buttonImage.size.height);
    [button addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];

    UIBarButtonItem *customBarItem = [[UIBarButtonItem alloc] initWithCustomView:button];
    self.navigationItem.leftBarButtonItem = customBarItem;
    [customBarItem release];
}

- (void)back {
    [self.navigationController popViewControllerAnimated:YES];
}

Make sure you have an button image with the size of a navigation bar back button in your resource folder with name back.png.

确保您的资源文件夹中有一个具有导航栏后退按钮大小的按钮图像,名称为 name back.png

Feel free if any other assistance is required.

如果需要任何其他帮助,请随意。

回答by Jagie

Target:customizing all back button on UINavigationBar to an white icon

目标:将UINavigationBar 上的所有后退按钮自定义为白色图标

Steps:1. in "didFinishLaunchingWithOptions" method of AppDelete:

步骤:1.在AppDelete的“didFinishLaunchingWithOptions”方法中:

UIImage *backBtnIcon = [UIImage imageNamed:@"navBackBtn"];

UIImage *backBtnIcon = [UIImage imageNamed:@"navBackBtn"];

if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"7.0")) {
    [UINavigationBar appearance].tintColor = [UIColor whiteColor];
    [UINavigationBar appearance].backIndicatorImage = backBtnIcon;
    [UINavigationBar appearance].backIndicatorTransitionMaskImage = backBtnIcon;
}else{

    UIImage *backButtonImage = [backBtnIcon resizableImageWithCapInsets:UIEdgeInsetsMake(0, backBtnIcon.size.width - 1, 0, 0)];
    [[UIBarButtonItem appearance] setBackButtonBackgroundImage:backButtonImage  forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];

    [[UIBarButtonItem appearance] setBackButtonTitlePositionAdjustment:UIOffsetMake(0, -backButtonImage.size.height*2) forBarMetrics:UIBarMetricsDefault];
}

2.in the "viewDidLoad" method of the common super ViewController class:

2.在普通超级ViewController类的“viewDidLoad”方法中:

 if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"7.0")) {
        UIBarButtonItem *backItem = [[UIBarButtonItem alloc] initWithTitle:@""
                                                                     style:UIBarButtonItemStylePlain
                                                                    target:nil
                                                                    action:nil];
        [self.navigationItem setBackBarButtonItem:backItem];
    }else{
        //do nothing
    }

回答by Michael

If you're doing this all over the place like I am, you're better off implementing Anil's solution as a category:

如果你像我一样到处这样做,你最好将 Anil 的解决方案作为一个类别来实施:

@interface UIViewController (CustomBackButton)

- (void) setCustomBackButton;
- (void) back;

@end

@implementation UIViewController (CustomBackButton)

- (void) setCustomBackButton
{
    UIImage *buttonImage = [UIImage imageNamed:@"back.png"];
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button setImage:buttonImage forState:UIControlStateNormal];
    button.frame = CGRectMake(0, 0, buttonImage.size.width, buttonImage.size.height);
    [button addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *customBarItem = [[UIBarButtonItem alloc] initWithCustomView:button];
    self.navigationItem.leftBarButtonItem = customBarItem;
}

- (void) back
{
    [self.navigationController popViewControllerAnimated:YES];
}

@end

回答by giuseppe

More simply:

更简单:

UIBarButtonItem *barBtnItem = 
  [[UIBarButtonItem alloc]initWithTitle:@"Indietro"
                                  style:UIBarButtonItemStyleBordered
                                 target:self
                                 action:@selector(pop)];
[barBtnItem setTintColor:[UIColor whiteColor]];
self.navigationItem.leftBarButtonItem = barBtnItem;

回答by simply_me

Suppose you have two controllers - Controller1 and Controller2. Controller2 is pushed from Controller1. So before pushing the Controller2 on the navigationController from Controller1

假设您有两个控制器 - Controller1 和 Controller2。Controller2 是从 Controller1 推送的。所以在从 Controller1 推送导航控制器上的 Controller2 之前

Controller2 *controller2 = [[[Controller2 alloc]  init]autorelease];
self.navigationItem.hidesBackButton = YES;   

Now, in the viewDidLoad: method of Controller2, add the following snippet

现在,在 Controller2 的 viewDidLoad: 方法中,添加以下代码段

UIBarButtonItem *backBarButtonItem =[[[UIBarButtonItem alloc]initWithTitle:@"Back" style:UIBarButtonItemStyleBordered target:self action:@selector(goBackToAllPets:)]autorelease];
self.navigationItem.leftBarButtonItem = backBarButtonItem;

and in the backButtonClicked method, you can perform the checks you want to.

在 backButtonClicked 方法中,您可以执行您想要的检查。