我想删除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;
但它不起作用!
答案 0 :(得分: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];
答案 1 :(得分:0)
UINavigation.backBarButtonItem
不用于设置当前视图控制器的后退按钮,它用于从当前视图控制器推送的控制器的后退按钮。
现在说ViewController1
正在UINavigationController
中显示,您设置了backBarButton
的{{1}}。后退按钮不会改变,但是当您按ViewController1
时,您会看到ViewController2
的后退按钮是您为ViewController2
设置的内容。
要设置ViewController1
的后退按钮,要么在ViewController中设置ViewController1
,要么用户ViewController1
,后者需要在YourButton上添加事件监听器
答案 2 :(得分: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];
}