我想用自定义图片替换我的iPhone应用程序中的返回按钮和整个导航栏。首先,这是否可能?其次,如果可能,如何实现?谢谢。
我想用自定义图片替换我的iPhone应用程序中的返回按钮和整个导航栏。首先,这是否可能?其次,如果可能,如何实现?谢谢。
这段代码将修改应用中的每个UINavigationBar。
@implementation UINavigationBar (CustomImage)
- (void)drawRect:(CGRect)rect {
UIImage *image = [UIImage imageNamed: @"MyNavigationBar.png"];
[image drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
}
@end
您可以使用此代码将导航栏图像和选项卡图像更改为自定义图像:
if([[UINavigationBar class] respondsToSelector:@selector(appearance)]) {//iOS >=5.0
UIImage *image = [UIImage imageNamed:@"myCustomImage.png"];
CGSize newSize = CGSizeMake(self.navigationController.navigationBar.frame.size.width, self.navigationController.navigationBar.frame.size.height);
UIGraphicsBeginImageContext(newSize);
[image drawInRect: CGRectMake(0, 0, self.navigationController.navigationBar.frame.size.width, self.navigationController.navigationBar.frame.size.height)];
UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
[self.navigationController.navigationBar setBackgroundImage:newImage forBarMetrics:UIBarMetricsDefault];
[[UIToolbar appearance] setBackgroundImage:newImage forToolbarPosition:UIToolbarPositionAny barMetrics:UIBarMetricsDefault];
[[UITabBar appearance] setBackgroundImage:newImage];
[[UITabBar appearance] setSelectedImageTintColor:[UIColor cyanColor]];
}
你无法轻松地覆盖苹果导航栏的外观和感觉,即使你能够这样做,很可能会违反他们避免使用私有API的政策。然而,使用所有自定义图像创建类似导航栏的外观非常简单。这对于需要导航栏的简单一次性屏幕非常有效,但在复杂的基于UINavigationController的层次结构上实现起来会很困难。
编辑:看起来人们已经通过覆盖-drawRect
选择器来更改背景:Custom background for UINavigationBar? 。我没有看到任何关于这些更改是否被App Store批准的报告。