我想为应用中的所有控制器设置自定义后退栏按钮。我试过用这个:
[[UIBarButtonItem appearance]
setBackButtonBackgroundImage:backButtonImage
forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
设置图像。没关系。但是,我真正想做的是,我只想要一个自定义按钮用于后栏按钮,它不包含任何标题等。上面的代码可以工作,但它会添加自动标题并调整后栏按钮项的大小。我需要为应用程序中的所有控制器设置一个固定的框架,无标题后栏按钮项。
答案 0 :(得分: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];
}
答案 1 :(得分:2)
之前我遇到过类似的问题,我到处寻找解决方案。
我找到的唯一一个适用于您的问题的方法是在每个视图控制器中实现一个执行弹出的UILeftBarButton。
您可以按照自己的方式更改背景图片,但如果您将文字设置为nil或空文本(“”),那么您的按钮就不会显示。
您也无法更改UIBackBarButton的视图,只能更改它的文本(因此没有自定义按钮)。
答案 2 :(得分:2)
我做了什么,使用外观代理将后退标题标签alpha设置为零。
答案 3 :(得分: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;
}
并在所有ur文件中导入此类别,并在所有类中调用setCustomBackButton。这对我来说很好,即使在iOS 7中也是如此。
在所有你的类的ViewDidLoad中。
- (void)viewDidLoad
{
[super viewDidLoad];
[self.navigationItem setCustomBackButton];
}
答案 4 :(得分: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];
}
答案 5 :(得分:0)
试试这段代码:
UIButton *tempbtn = [[UIButton alloc] init];
[tempbtn setImage:[UIImage imageNamed:@"testbtn.png"] forState:UIControlStateNormal];
UIBarButtonItem *temp = [[UIBarButtonItem alloc] initWithCustomView:tempbtn];
self.navigationItem.rightBarButtonItem = temp;