ios 5将导航控制器中后退按钮的背景更改为透明 [英] ios 5 change the background of back button in navigation controller to transparent

查看:80
本文介绍了ios 5将导航控制器中后退按钮的背景更改为透明的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用背景图像自定义了导航控制器标题栏,但我真的很难将后退按钮的背景颜色更改为透明,以便与其下方的绿色标题栏匹配。我是iOS开发的新手。任何人都可以建议可以做什么?

I have customised the navigation controller title bar with a background image, but I am really struggling to change the background color of the back button to transparent so that it matches with the green title bar beneath it. I am fairly new to iOS development. Can any one suggest what could be done?

以下是我用来更改导航控制器标题栏的代码,以防它有用:

Here is the code for I used to change the title bar of navigation controller, just in case it helps:

- (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

}

编辑:经过一番研究后,我设法得到了我想要的东西。以下是更改后退按钮背景图像的代码:

EDIT: After doing a bit of research I managed to get what I wanted. Here is the code to change the background image for the back button:

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

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

The above code adds the image to all the back buttons in the navigation controller.

推荐答案

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

You can't change the appearance of the default back button, but you can create your own button to replace it.

- (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将导航控制器中后退按钮的背景更改为透明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆