NavigationBar setShadowImage并不总是有效 [英] NavigationBar setShadowImage not always working

查看:800
本文介绍了NavigationBar setShadowImage并不总是有效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在表格视图中为导航栏设置自定义阴影图像,但它仅在某些视图中显示。我已经创建了一个超类来为我的表视图设置样式。

I'm trying to set a custom shadow image for the navigation bar in my table views, but it's only showing in some views. I've created a super class to set the styles for my table views.

- (void)viewDidLoad
{
    [super viewDidLoad];

    // Set navigation bar background
    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"navigationbarbackground.png"] forBarMetrics:UIBarMetricsDefault];

    // Set navigation bar shadow imag
    [self.navigationController.navigationBar setShadowImage:[UIImage imageNamed:@"navigationbarshadow.png"]];

在启动我的应用程序时看到的视图中,没有显示阴影。但是当我触摸导航栏中的[+]按钮打开我的'添加新项目'表格视图时,它确实会显示阴影。

In the view I see at starting my app, no shadow is showed. But when I touch the [+] button in my navigation bar to open my 'add new item' table view, it does show a shadow.

有人能指出我在正确的方向吗?

Could someone point me in the right direction here?

推荐答案

外观代理应该有效。

在启动时只需在某处(例如在你的AppDelegate中)调用它。

Just call it somewhere (e.g. in your AppDelegate) upon startup.

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{ 
    [self customizeAppearance];
    return YES;
}

- (void) customizeAppearance 
{
    // Set the background image for *all* UINavigationBars
    [[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:@"navigationbarbackground"] forBarMetrics:UIBarMetricsDefault];

    // Set the shadow image for *all* UINavigationBars
    [[UINavigationBar appearance] setShadowImage:[UIImage imageNamed:@"navigationbarshadow.png"]];  

    //add other appearance stuff here...
}

但是如果你创建一个包含多个UINavigationController的故事板和一堆segue推送导航控制器,你可能会得到一个腐败的视图控制器结构,这可能是这里的问题。

However if you create a storyboard with multiple UINavigationController's in it and a bunch of segue's pushing navigation controller's you might get a corrupt view controller structure which might be the problem here.

另一个可能的问题可能是您的nib文件或故事板中某处的导航栏的剪辑子视图选项。如果你想要阴影(图像),请确保它已关闭!

Another possible issue might be the Clip Subviews option of a Navigation Bar somewhere in your nib file or you storyboard. Make sure it is turned off if you want the shadow (image)!

顺便说一句,如果您使用imageNamed,则不需要包含文件扩展名。

By the way, if you use imageNamed you don't need to include the file extension.

这篇关于NavigationBar setShadowImage并不总是有效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆