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

时间:2011-11-22 03:21:38

标签: iphone ios uinavigationcontroller uinavigationitem

在我的应用程序中,有许多UIViewControllersUINavigationControllersUIButton上必须有一个“后退”按钮和一个“主页”UINavigationBar。所有这一切都很好。

但是我的一些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];

我需要“返回”按钮才能有这样的风格: Draw custom Back button on iPhone Navigation Bar

6 个答案:

答案 0 :(得分:50)

尝试以下方法。它一定会奏效:

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

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

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

答案 1 :(得分:7)

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

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

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.在常见的超级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
    }

答案 2 :(得分:6)

试试这个

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

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

答案 3 :(得分:2)

如果你像我一样在各地这样做,你最好将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

答案 4 :(得分:1)

更简单:

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

答案 5 :(得分:0)

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

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

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

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

并且在backButtonClicked方法中,您可以执行所需的检查。