简体   繁体   中英

How to set the background of UINavigationBar in Objective-C ?

I am trying to change the background of UINavigationBar .

I already put the image to Images.xcassets , and the xib file is TableView

在此输入图像描述

I have write [[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:@"bar-background.png"] forBarMetrics:UIBarMetricsDefault]; in the viewDidLoad like the following.

    - (void)viewDidLoad
    {
        [super viewDidLoad];
        // Do any additional setup after loading the view from its nib.

        UIView* bview = [[UIView alloc] init];
        bview.backgroundColor = [UIColor groupTableViewBackgroundColor];

        //self.title = NSLocalizedString(@"WiFi Camera Viewer", nil) ;
        [[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:@"bar-background.png"] forBarMetrics:UIBarMetricsDefault];
}

But the UINavigationBar did not show any image like the following 在此输入图像描述

How to set the background of UINavigationBar in Objective-C ?

Did I missing something ? Thanks in advance.

The code you are using is old, starting from iOS 7 you could use the below

[self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"YourImage"] forBarMetrics:UIBarMetricsDefault];

If you provide an image with 44px height, it'll cover the navigation bar only without the status bar, if you provide it with 66px height, it'll cover them both

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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