设置自定义后台button通用,没有标题

我想为应用程序中的所有控制器设置自定义后退button。 我试着用这个:

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

它设置图像。 没关系。 但是,我真正想要做的是,我只想要一个自定义的button,不包含任何标题等back barbutton。上面的代码工作,但它增加了自动标题,并调整后栏button项目。 我的需求是有一个固定的框架,没有标题的应用程序中的所有控制器后面的button项。

我解决了它。 只需在UIViewController上创build一个类别并将其导入到prefix.pch文件中即可。 然后写一个方法:customViewWillAppear:并用viewWillAppear方法debugging它:

 +(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]; } 

现在,对于代码中的每个控制器,都有一个自定义后退button。 这花了我很多时间来执行和弄清楚。 希望它也能帮助你们。

编辑:请使用下面的代码来支持iOS7>回刷function;

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

创build一个基本视图控制器并添加下面的代码;

 - (void)viewDidLoad { [super viewDidLoad]; self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"" style:UIBarButtonItemStylePlain target:nil action:nil]; } 

之前我也遇到过类似的问题,我到处寻找解决办法。

我find的唯一一个适用于你的问题的方法是在每个视图控制器中实现一个UILeftBarButton,它popup窗口。

你可以按照自己的方式来改变背景图片,但是如果你设置的文本为零或空文本(“”),那么button就不会显示出来。

您也不能更改UIBackBarButton的视图,只有它的文本(所以没有自定义button)。

我做了什么,使用外观代理将后退标题标签alpha设置为零。

我在ViewDidLoad:代码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]; } 

你可以写这样的类别,

 // // 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]; } 

试试这个代码:

 UIButton *tempbtn = [[UIButton alloc] init]; [tempbtn setImage:[UIImage imageNamed:@"testbtn.png"] forState:UIControlStateNormal]; UIBarButtonItem *temp = [[UIBarButtonItem alloc] initWithCustomView:tempbtn]; self.navigationItem.rightBarButtonItem = temp;