IOS 5变回按钮的背景在导航控制器到透明(ios 5 change the background

2019-06-24 06:16发布

我已经自定义的背景图片导航控制器标题栏,但我真的很挣扎后退按钮的背景颜色变为透明,使之与它下面的绿色标题栏相匹配。 我是相当新的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];

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

Answer 1:

您不能更改默认的返回按钮的外观,但你可以创建自己的按钮来取代它。

- (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];
    }
}


文章来源: ios 5 change the background of back button in navigation controller to transparent