0

我想删除 UIViewController 后退按钮的背景。

我怎样才能做到这一点?

我尝试了以下代码

backButton = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"back.png"] landscapeImagePhone:nil style:UIBarButtonSystemItemCancel target:self action:nil];
    [backButton setBackgroundImage:nil forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
self.navigationItem.backBarButtonItem = backButton;

但它没有用!

4

3 回答 3

1

不要尝试修改UINavigationBar的后退按钮图像。这是不可能的,因为这是默认的。

而是将另一个自定义项UIButton作为UINavigationBar项目,然后您可以根据需要对其进行自定义。

UIButton *btnItem = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 30, 30)];
[btnItem setTitle:@"title" forState:UIControlStateNormal];
[btnItem addTarget:self action:@selector(btnItem_click:) forControlEvents:UIControlEventTouchUpInside];
self.topItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:btnItem];
于 2013-10-08T10:41:03.860 回答
0

UINavigation.backBarButtonItem不用于设置当前视图控制器的后退按钮,它用于设置从当前视图控制器推送的控制器的后退按钮

说现在ViewController1正在放映中UINavigationController,你定backBarButtonViewController1。后退按钮不会改变,但是你按下ViewController2后,你会看到ViewController2你设置的后退按钮ViewController1

要设置后退按钮ViewController1,要么在 ViewController push 中设置,要么在ViewController1用户中设置self.navigationItem.leftBarbuttonItem = YourButton,后一种方式需要在 YourButton 上添加事件侦听器

于 2013-10-08T10:34:35.273 回答
0

这段代码对我有用

UIImage *backButtonImage = [UIImage imageNamed:@"back.png"];
UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
[backButton addTarget:self action:@selector(backButtonPressed) forControlEvents:UIControlEventTouchUpInside];
[backButton setBackgroundImage:backButtonImage forState:UIControlStateNormal];
[backButton setFrame:CGRectMake(0, 0, 30, 30)];
UIBarButtonItem *backBarButton = [[UIBarButtonItem alloc] initWithCustomView:backButton];
[backBarButton setAction:@selector(backButtonPressed)];
self.navigationItem.leftBarButtonItem = backBarButton;
-(void)backButtonPressed{
    [self.navigationController popViewControllerAnimated:YES];
}
于 2013-10-09T12:54:18.363 回答