6

我已经使用背景图像自定义了导航控制器标题栏,但我真的很难将后退按钮的背景颜色更改为透明,以便它与下方的绿色标题栏匹配。我对 iOS 开发相当陌生。有人可以建议可以做什么吗?

这是我用来更改导航控制器标题栏的代码,以防万一:

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    if ([self.navigationController.navigationBar respondsToSelector:@selector(setBackgroundImage:forBarMetrics:)] ) {
        UIImage *image = [UIImage imageNamed:@"greenbar.png"];
        [self.navigationController.navigationBar setBackgroundImage:image forBarMetrics:UIBarMetricsDefault];
       // [[UIBarButtonItem appearance] setBackButtonBackgroundImage:image forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];

    } 

    //change back button image

}

编辑:在做了一些研究之后,我设法得到了我想要的东西。这是更改后退按钮背景图像的代码:

 UIImage *image1 = [UIImage imageNamed:@"back-bt.png"];
    [[UIBarButtonItem appearance] setBackButtonBackgroundImage:image1 forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];

上面的代码将图像添加到导航控制器中的所有后退按钮。

4

1 回答 1

5

您无法更改默认后退按钮的外观,但您可以创建自己的按钮来替换它。

- (void)viewDidLoad {

    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    if ([self.navigationController.navigationBar respondsToSelector:@selector(setBackgroundImage:forBarMetrics:)] ) {
        UIImage *image = [UIImage imageNamed:@"greenbar.png"];
        [self.navigationController.navigationBar setBackgroundImage:image forBarMetrics:UIBarMetricsDefault];
       // [[UIBarButtonItem appearance] setBackButtonBackgroundImage:image forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];

    } 

    //change back button image
    if(self.navigationController.viewControllers.count > 1) {
        UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];

        [backButton setTitle:@"Back" forState:UIControlStateNormal];
        [backButton addTarget:self action:@selector(didTapBackButton:) forControlEvents:UIControlEventTouchUpInside];
        backButton.frame = CGRectMake(0.0f, 0.0f, 64.0f, 41.0f);
        UIBarButtonItem *backButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];

        self.navigationItem.leftBarButtonItem = backButtonItem;
    }
}



- (void) didTapBackButton:(id)sender {
    if(self.navigationController.viewControllers.count > 1) {
        [self.navigationController popViewControllerAnimated:YES];
    }
}
于 2012-05-08T10:20:11.070 回答