5

我想为应用程序中的所有控制器设置自定义后退栏按钮。我尝试使用这个:

[[UIBarButtonItem appearance] setBackButtonBackgroundImage:backButtonImage forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];

它设置图像。没关系。但是,我真正想做的是,我只想要一个不包含任何标题等的后栏按钮的自定义按钮。上面的代码有效,但它添加了自动标题并调整后栏按钮项目的大小。我的需要是为应用程序中的所有控制器提供一个固定框架、无标题的后退栏按钮项。

4

6 回答 6

5

我已经解决了。只需在 UIViewController 上创建一个类别并将其导入 prefix.pch 文件。然后编写一个方法:customViewWillAppear: 并使用 viewWillAppear 方法对其进行调整:

+(void)load{

Method viewWillAppear = class_getInstanceMethod(self, @selector(customViewWillAppear:));

Method customViewWillAppear = class_getInstanceMethod(self, @selector(viewWillAppear:));
method_exchangeImplementations(viewWillAppear, customViewWillAppear);

}

将上述方法添加到该类别类。然后像这样实现您的 customViewWillAppear 方法:

-(void)customViewWillAppear:(BOOL)animated{
    [self customViewWillAppear:animated];
    if([self.navigationController.viewControllers indexOfObject:self] != 0  && !self.navigationItem.hidesBackButton){
        UIBarButtonItem *cancelBarButton = nil;
        UIButton* cancelButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [cancelButton addTarget:self action:@selector(popViewControllerWithAnimation) forControlEvents:UIControlEventTouchUpInside];
        [cancelButton setBackgroundImage:[UIImage imageNamed:@"yourImage.png"] forState:UIControlStateNormal];
        [cancelButton sizeButtonToFit];

        cancelBarButton = [[UIBarButtonItem alloc] initWithCustomView:cancelButton];

        NSMutableArray * leftButtons = [NSMutableArray arrayWithObject:cancelBarButton];
        [leftButtons addObjectsFromArray:self.navigationItem.leftBarButtonItems];
        [self.navigationItem setLeftBarButtonItem:nil];
        [self.navigationItem setLeftBarButtonItems:leftButtons];
    }

    [self.navigationItem setHidesBackButton:YES];
}
-(void)popViewControllerWithAnimation{
    [self.navigationController popViewControllerAnimated:YES];
}

现在,对于代码中的每个控制器,您都有一个自定义的后退按钮。这花了我很多时间来实施和弄清楚。希望对大家也有帮助。

编辑:请使用以下代码支持 iOS7> 后滑动功能;

UIImage *image = [UIImage imageForName:@"some_image"];
navBar.backIndicatorImage = [image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
navBar.backIndicatorTransitionMaskImage = [image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];

创建一个基本视图控制器并添加以下代码;

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"" style:UIBarButtonItemStylePlain target:nil action:nil];
}
于 2013-09-17T13:37:40.497 回答
2

我以前遇到过类似的问题,我到处寻找解决方案。

我发现的唯一一个适用于您的问题的方法是在每个视图控制器中实现一个 UILeftBarButton 来执行弹出操作。

您可以按照您的方式更改背景图像,但如果您将文本设置为 nil 或空文本 (""),您的按钮将不会显示。

您也无法更改 UIBackBarButton 的视图,只能更改文本(因此没有自定义按钮)。

于 2013-08-07T09:05:56.030 回答
2

我所做的是使用外观代理将后退按钮标题标签 alpha 设置为零。

于 2013-08-07T09:07:10.887 回答
1

你可以写一个这样的类别,

//
//  UINavigationItem+BarAditions.h
//
//  Created by Satheeshwaran on on 7/5/13.
//


#import <Foundation/Foundation.h>


@interface UINavigationItem (PersonaAddition)

- (void)setCustomBackButton;

@end


//
//  UINavigationItem+BarAditions.m
//
//  Created by Satheeshwaran on on 7/5/13.
//

#import "UINavigationItem+PersonaAddition.h"


@implementation UINavigationItem (PersonaAddition)

- (void)setCustomBackButton 
{
    //customize ur back button here.
    UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
    backButton.frame=CGRectMake(0, 0, 60, 30);
    [backButton addTarget:target action:@selector(didPressLeftItem:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *barItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];
    self.leftBarButtonItem = barItem;    

}

并在您的所有文件中导入此类别,并在您的所有类中调用 setCustomBackButton。这对我来说很好,即使在 iOS 7 中也是如此。

在所有类的 ViewDidLoad 中。

- (void)viewDidLoad
{
    [super viewDidLoad];

    [self.navigationItem setCustomBackButton];

}
于 2013-08-07T09:23:08.020 回答
1

我的代码在ViewDidLoad:

// Set back button image
if ([[self.navigationController viewControllers] objectAtIndex:0] != self) {
    UIButton *btnBack = [UIButton buttonWithType:UIButtonTypeCustom];
    btnBack.frame = CGRectMake(0,0,38,30);
    [btnBack setBackgroundImage:[UIImage imageNamed:@"NavigationBarBackButton"] forState:UIControlStateNormal];
    [btnBack addTarget:self.navigationController action:@selector(popViewControllerAnimated:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *leftBtn = [[UIBarButtonItem alloc] initWithCustomView:btnBack];
    [self.navigationItem setLeftBarButtonItem:leftBtn];
}
于 2013-09-18T09:35:33.080 回答
0

试试这个代码:

UIButton *tempbtn = [[UIButton alloc] init];
[tempbtn setImage:[UIImage imageNamed:@"testbtn.png"] forState:UIControlStateNormal];
UIBarButtonItem *temp = [[UIBarButtonItem alloc] initWithCustomView:tempbtn];
self.navigationItem.rightBarButtonItem = temp;
于 2013-08-07T09:07:42.233 回答