Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Background Image for UINavigationBar

I am trying to set the background image for UINavigationBar.

I tried 2 types of methods,

 1. [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"KnavigationBarImage.png"] forBarMetrics:UIBarMetricsDefault];

It is supported in ios 5, but crashes when run it in ios 4.3.

 2. self.navigationController.navigationBar.layer.contents = (id)[UIImage imageNamed:@"KnavigationBarImage.png"].CGImage;

It is working fine in ios 4 , But the image is not displaying in ios 5 Simulator.

I need the code which should work in both version 4 and 5.

Please suggest me.

Thanks.

like image 467
Bharathi Avatar asked Dec 03 '25 02:12

Bharathi


1 Answers

Write a method like this to find which OS the device has.

static int sIsiOS5 = -1;
+(BOOL)isIOS5
{
    if( -1 == sIsiOS5)
    {
        sIsiOS5 = 0;
        if( [[UIScreen mainScreen] respondsToSelector:@selector(brightness)])
        {
            sIsiOS5 = 1;
        }
    }
    return sIsiOS5;
}

While setting the image, check as following and then set the image.

if ( [Utilities isIOS5] ) {
    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"KnavigationBarImage.png"] forBarMetrics:UIBarMetricsDefault];
}
else
{
    self.navigationController.navigationBar.layer.contents = (id)[UIImage imageNamed:@"KnavigationBarImage.png"].CGImage;
}
like image 130
Ilanchezhian Avatar answered Dec 05 '25 17:12

Ilanchezhian



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!