Martin
Martin

Reputation: 2863

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

enter image description here

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 enter image description here

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

Did I missing something ? Thanks in advance.

Upvotes: 1

Views: 112

Answers (1)

user1453351
user1453351

Reputation:

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

Upvotes: 3

Related Questions