0

当我的一个应用程序从一个控制器导航到另一个控制器时,“返回” UIBarButtonItem 消失了,所以我编写了代码:

        UIBarButtonItem *barButton1 = [[UIBarButtonItem alloc] initWithTitle:@""
                                                                       style:UIBarButtonItemStyleDone
                                                                      target:self
                                                                      action:@selector(barButtonItemPressed:)];
        self.navigationItem.leftBarButtonItem = barButton1;
        [barButton1 setImage:[UIImage imageNamed:@"fdj.png"]];
        [barButton1 release];

它有效,但显示为

在此处输入图像描述

但我更喜欢这种风格(比如标准的 go back barbuttonitem

在此处输入图像描述

可能吗?

欢迎任何评论

4

4 回答 4

2

您应该编辑navigationItem父视图控制器的:

你可以把它放在viewDidLoad前一个视图控制器中:

 self.navigationItem.backBarButtonItem = [[[UIBarButtonItem alloc] initWithTitle:nil style:UIBarButtonItemStylePlain target:nil action:nil] autorelease];
 self.navigationItem.backBarButtonItem.image = [UIImage imageNamed:@"fdj.png"];
于 2012-05-29T09:37:31.173 回答
0

这是您如何执行此操作的代码

UIButton *btnBack = [UIButton buttonWithType:UIButtonTypeCustom];
btnBack.frame = CGRectMake(0, 0, 55, 36);
[btnBack setImage:[UIImage imageNamed:@"btnBack"] forState:UIControlStateNormal];
[btnBack setImage:[UIImage imageNamed:@"btnBack"] forState:UIControlStateSelected];
[btnBack addTarget:self action:@selector(onClickBack) forControlEvents:UIControlEventTouchUpInside];

UIBarButtonItem *item4 = [[UIBarButtonItem alloc] initWithCustomView:btnBack];

self.navigationItem.leftBarButtonItem = item4;

只需制作图像并将其设置在 uibutton 中,这是执行此操作的简单方法。你可以设置任何你想要的图像。希望这可以帮助你想要什么。

于 2012-05-29T09:40:26.337 回答
0

视图控制器的init方法中,设置

self.navigationItem.backBarButtonItem = [[[UIBarButtonItem alloc] initWithImage: [UIimage imageNamed: @"fdj.png"] 
                                                                          style: UIBarButtonItemStyleBordered 
                                                                         target: nil 
                                                                         action: nil] autorelease];
于 2012-05-29T09:40:37.307 回答
-2

这是我在我的一个旧项目中使用的(是的,我知道它stretchableImageWithLeftCapWidth已被弃用)。

@implementation UIViewController (UIView_ExtenderClass) // this is a category, obviously

...

-(void)setLeftButton:(id)theTarget navItem:(UINavigationItem *)myNavItem action:(SEL)myAction title:(NSString *)myTitle;
{
  CGSize titleWidth = [myTitle sizeWithFont:[UIFont boldSystemFontOfSize:14.0]];
  UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, titleWidth.width + 20, 32)];
  [button setTitle: [NSString stringWithFormat:@"   %@", myTitle] forState:UIControlStateNormal];
  button.titleLabel.font = [UIFont boldSystemFontOfSize:14.0];

  [button addTarget:theTarget action:myAction forControlEvents:UIControlEventTouchUpInside];

  UIImage *imgBack = [UIImage imageNamed:@"32_left_nav_bar.png"];
  UIImage *imgBackStretched = [imgBack stretchableImageWithLeftCapWidth:15 topCapHeight:0];
  UIImage *imgBackSelected = [UIImage imageNamed:@"32_left_nav_bar_selected.png"];
  UIImage *imgBackSelectedStretched = [imgBackSelected stretchableImageWithLeftCapWidth:15 topCapHeight:0];

  [button setBackgroundImage:imgBackStretched forState:UIControlStateNormal];
  [button setBackgroundImage:imgBackSelectedStretched forState:UIControlStateHighlighted];

  UIBarButtonItem *buttonItem = [[UIBarButtonItem alloc] initWithCustomView:button];
  [buttonItem setTarget:theTarget];

  myNavItem.leftBarButtonItem = nil;
  myNavItem.leftBarButtonItem = buttonItem;

  [imgBack release];
  [imgBackSelected release];

  [button release];
  [buttonItem release];
}

viewDidLoad在第二个控制器中调用它,如下所示:

[self setLeftButton:self navItem:self.navigationItem action:@selector(yourActionHere) title:@"Back"]

我使用的图片在这里:http: //i49.tinypic.com/6qfzfs.png

于 2012-05-29T10:41:23.127 回答