设置UINavigation的背景图片和背景颜色

时间:2023-03-10 01:49:51
设置UINavigation的背景图片和背景颜色
//通过背景图片来设置背景
float systemVersion = [[[UIDevice currentDevice] systemVersion] floatValue];
UIImage *backgroundImage = [UIImage imageNamed:@"navbg.png"]; //获取图片 if(systemVersion>=5.0)
{
CGSize titleSize = self.navigationController.navigationBar.bounds.size; //获取Navigation Bar的位置和大小
backgroundImage = [self scaleToSize:backgroundImage size:titleSize];//设置图片的大小与Navigation Bar相同
[self.navigationController.navigationBar setBackgroundImage:backgroundImage forBarMetrics:UIBarMetricsDefault]; //设置背景
}
else
{
[self.navigationController.navigationBar insertSubview:[[[UIImageView alloc] initWithImage:backgroundImage] autorelease] atIndex:1];
} //调整图片大小
- (UIImage *)scaleToSize:(UIImage *)img size:(CGSize)size{
UIGraphicsBeginImageContext(size);
[img drawInRect:CGRectMake(0, 0, size.width, size.height)];
UIImage* scaledImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return scaledImage;
} //设置背景样式可用通过设置tintColor来设置
self.navigationController.navigationBar.tintColor = [UIColor colorWithRed:50/255.0 green:138/255.0 blue:233/255.0 alpha:1.0];//改变navigation的背景颜色