我有一个ViewController,可以通过
打开另一个ViewController[self.navigationController pushViewController:detailViewController animated:YES];
现在新的VC在导航栏中有一个后退按钮,但是我无法设法为它提供与第一个ViewController中的navBar按钮相同的背景图像。我试过了:
UIImage *buttonImage = [UIImage imageNamed: @"Button.png"];
[self.navigationItem.backBarButtonItem setBackButtonBackgroundImage:buttonImage forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
[self.navigationItem.backBarButtonItem setTitle:@"doesnt work either"];
但它不起作用。我究竟做错了什么?谢谢!
答案 0 :(得分: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;
享受编码:)
答案 1 :(得分: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];
}