简体   繁体   English

自定义导航栏后退按钮,仅带图像

[英]Custom navigation bar back button with only image

I want to show custom navigation bar back button. 我想显示自定义导航栏后退按钮。 I want to show only image on it. 我想只显示图像。 I want to use this button in whole app also dont want to create this button in each file. 我想在整个应用程序中使用此按钮也不想在每个文件中创建此按钮。 How can i?? 我怎样才能??

Here is my code: 这是我的代码:

// Set the custom back button
        UIImage *buttonImage = [UIImage imageNamed:@"back_arrow.png"];

        //create the button and assign the image
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        [button setImage:buttonImage forState:UIControlStateNormal];

        //set the frame of the button to the size of the image (see note below)
        button.frame = CGRectMake(0, 0, buttonImage.size.width, buttonImage.size.height);

        [button addTarget:self action:@selector(popViewControllerAnimated:) forControlEvents:UIControlEventTouchUpInside];

        //create a UIBarButtonItem with the button as a custom view
        UIBarButtonItem *customBarItem = [[UIBarButtonItem alloc] initWithCustomView:button];
        self.navigationItem.leftBarButtonItem = customBarItem;

but this is showing on current page only. 但这仅在当前页面上显示。

Another code: 另一个代码:

[[UIBarButtonItem appearance] setBackButtonBackgroundImage:[UIImage imageNamed:@"back_arrow.png"] forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];

Using this code back button image is showing in whole app. 使用此代码后退按钮图像显示在整个应用程序中。 But the text "Back" is also showing. 但文字“后面”也在显示。 How can i solve this problem. 我怎么解决这个问题。

Thanks in advance. 提前致谢。

I've used a category in the past to do this. 我过去曾用过一个类别来做这件事。

Create a category on UIBarButtonItem called +projectButtons (or something). 在UIBarButtonItem上创建一个名为+ projectButtons(或其他东西)的类别。

Then you can have a function like... 然后你可以有一个像...这样的功能

+ (UIBarButtonItem)backArrowButtonWithTarget:(id)target action:(SEL)action
{
    UIImage *buttonImage = [UIImage imageNamed:@"back_arrow.png"];

    //create the button and assign the image
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button setImage:buttonImage forState:UIControlStateNormal];

    //set the frame of the button to the size of the image (see note below)
    button.frame = CGRectMake(0, 0, buttonImage.size.width, buttonImage.size.height);

    [button addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];

    //create a UIBarButtonItem with the button as a custom view
    UIBarButtonItem *customBarItem = [[UIBarButtonItem alloc] initWithCustomView:button];
    return customBarItem;
}

Then add it to you navigation bar like so... 然后将其添加到导航栏中,就像这样......

self.navigationItem.leftBarButtonItem = [UIBarButtonItem backArrowButtonWithTarget:self action:@selector(popViewControllerAnimated:)];

This means you only need one line of code to create it but you still get the customisation of the target and action in each VC that you use it in. If you decide to change the look of the button then it's all done in one place. 这意味着您只需要一行代码来创建它,但您仍然可以在您使用它的每个VC中自定义目标和操作。如果您决定更改按钮的外观,那么它们都在一个地方完成。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM