状态栏中的iOS 4.3透明png不透明

时间:2012-05-10 09:58:35

标签: iphone ios ios4

我在使用png图像的透明位时遇到一些困难,我正在使用它替换默认状态栏,以便在iOS 4.3中呈现为透明状态。目前他们正在变黑。

这是我用来绘制图像的代码:

@implementation UINavigationBar (BackgroundImage)
    - (void)drawRect:(CGRect)rect 
    {
        UIImage *image = nil;

        switch(self.tag)
        {
            case HeaderBG_Logo:
                image = [UIImage imageNamed: @"top_bar_logo.png"];
                break;
            case HeaderBG_Plain:
                image = [UIImage imageNamed: @"top_bar.png"];
                break;
        }

        if(image != nil)
        {
            [image drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
        }

    }
@end

以下是在ios4.3和ios5中模拟器中运行的相同应用程序的并排比较(图像的底部边框有阴影): screenshot

另外,4.3背景图像似乎也没有像5.0那样高。

我尝试将UINavigationBar设置为opaque /将其背景颜色设置为clearColor。这些都没有效果。 :(

有人可以帮忙吗?

3 个答案:

答案 0 :(得分:1)

我最终使用How to create UINavigationBar drop shadow中的解决方案为UINavigationBar添加了阴影。

答案 1 :(得分:0)

只是提示,对于iOS 5,您可以使用新的appreance属性来设置背景图像:

[[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:@"top_bar.png"] forBarMetrics:UIBarMetricsDefault];

这将为应用中的所有UINavigationBar设置图片。

答案 2 :(得分:0)

我不确定您是否关注状态栏或navigationBar。根据你的问题,似乎你更关心导航,所以这里有一个示例代码,为我添加iOS5和iOS4的背景图像。它适用于透明的png。

只需在viewController的loadView方法中添加:

#define kBackgroundImageTag 42

UINavigationBar *theNavigationBar = self.navigationController.navigationBar;
UIImage *myBackgroundImage = [UIImage imageNamed:@"myImage.png"];

if([theNavigationBar respondsToSelector:@selector(setBackgroundImage:forBarMetrics:)]) //iOS5 stuff
{
     [theNavigationBar setBackgroundImage:myBackgroundImage forBarMetrics: UIBarMetricsDefault];

}
else if(![theNavigationBar viewWithTag:kBackgroundImageTag]) //iOS 4 stuff
{
     UIImageView *imageView = [[UIImageView alloc] initWithImage:myBackgroundImage];
     imageView.tag = kBackgroundImageTag;
     [theNavigationBar insertSubview:imageView atIndex:0];
}

希望它有所帮助!

相关问题