简体   繁体   English

iPhone导航栏图像

[英]iPhone Navigation Bar Images

I'd like to replace the back button and the entire navigation bar in my iPhone app with custom images. 我想用自定义图像替换我的iPhone应用程序中的后退按钮和整个导航栏。 First of all, is this possible? 首先,这可能吗? Second, if so, how? 第二,如果是这样,怎么样? Thanks. 谢谢。

This code will modify every UINavigationBar in your app. 此代码将修改您应用中的每个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

You can use this code to change navigation bar image and tab bar image to your custom images: 您可以使用此代码将导航栏图像和标签栏图像更改为自定义图像:

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

You won't be able to override the look and feel of Apple's navigation bar easily, and even if you are able to, there's a good chance it will go against their policy of avoiding private API's. 您将无法轻松覆盖Apple导航栏的外观,即使您能够,也很有可能违反其避免私有API的政策。 However, it is pretty simple to create your own look-alike navigation bar using all custom images. 但是,使用所有自定义图像创建自己的外观相似的导航栏非常简单。 This would work well for simple one-off screens that need the navigation bar, but will be a beast to implement on a complex UINavigationController-based hierarchy. 这适用于需要导航栏的简单一次性屏幕,但是在基于UINavigationController的复杂层次结构上实现它将是一种野兽。

Edit: It looks like people have been able to change the background by overriding the -drawRect selector: Custom background for UINavigationBar? 编辑:看起来人们已经能够通过覆盖-drawRect选择器来改变背景: UINavigationBar的自定义背景? . I don't see any reports of whether or not the changes were approved for the App Store. 我没有看到任何关于这些更改是否已获得App Store批准的报告。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM