1

我有一个 ViewController 通过它打开另一个 ViewController

[self.navigationController pushViewController:detailViewController animated:YES];

现在新的 VC 在 navBar 中有一个后退按钮,但我无法为它提供与第一个 ViewController 中的 navBar 按钮相同的背景图像。我试过:

UIImage *buttonImage = [UIImage imageNamed: @"Button.png"];
[self.navigationItem.backBarButtonItem setBackButtonBackgroundImage:buttonImage forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
[self.navigationItem.backBarButtonItem setTitle:@"doesnt work either"];

但它不起作用。我究竟做错了什么?谢谢!

4

2 回答 2

2

试试下面给出的代码片段,它对我来说很好:

UIBarButtonItem *barBtnItem = [[UIBarButtonItem alloc]initWithTitle:@"Back" style:UIBarButtonItemStyleBordered target:self action:@selector(pop)];

self.navigationItem.backBarButtonItem = barBtnItem;

或者你也可以这样做:

   UIButton *backButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 26, 26)];
   [backButton setImage:[UIImage imageNamed:@"back"] forState:UIControlStateNormal];
   [backButton setShowsTouchWhenHighlighted:TRUE];
   [backButton addTarget:self action:@selector(popViewControllerWithAnimation) forControlEvents:UIControlEventTouchDown];
   UIBarButtonItem *barBackItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];
   self.navigationItem.hidesBackButton = TRUE;
   self.navigationItem.leftBarButtonItem = barBackItem;

享受编码:)

于 2013-01-14T09:36:49.260 回答
1

试试这个:

        UIButton *button =  [UIButton buttonWithType:UIButtonTypeCustom];
        [button setImage:[UIImage imageNamed:@"Button.png"] forState:UIControlStateNormal];
        [button addTarget:self action:@selector(pushNav:) forControlEvents:UIControlEventTouchUpInside];
        [button setFrame:CGRectMake(0, 0, 32, 32)];
         self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:button];

      -(void)pushNav {
           [self.navigationController popViewControllerAnimated:YES];  
      }
于 2013-01-14T09:37:29.960 回答