iPhone导航栏图像

4

我想用自定义图片替换我的iPhone应用程序中的返回按钮和整个导航栏。首先,这是否可能?其次,如果可能,如何实现?谢谢。


1
您是指为导航栏上的按钮定制图像,还是整个导航栏(背景等)? - Marc W
我也对按钮感兴趣 =) - Pasta
3个回答

17

这段代码将修改应用中的每个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

@pierre 很好的回答,如果你想保留返回按钮,最好给超级管理员打个电话确认一下。 - Oscar Gomez

0

您可以使用此代码将导航栏图像和选项卡图像更改为自定义图像:

   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]];
}

0

你无法轻松地覆盖苹果导航栏的外观和感觉,即使你能够这样做,很可能会违反他们避免使用私有API的政策。然而,使用所有自定义图像创建类似导航栏的外观非常简单。这对于需要导航栏的简单一次性屏幕非常有效,但在复杂的基于UINavigationController的层次结构上实现起来会很困难。

编辑:看起来人们已经通过覆盖-drawRect选择器来更改背景:Custom background for UINavigationBar? 。我没有看到任何关于这些更改是否被App Store批准的报告。


2
我见证了许多涉及这种做法的应用程序被批准在商店出售。苹果并不关心。 - Şafak Gezer

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接