简体   繁体   English

ios 5将导航控制器中后退按钮的背景更改为透明

[英]ios 5 change the background of back button in navigation controller to transparent

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. 我是iOS开发的新手。 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];
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM