NavigationBar setShadowImage not always working

2019-03-30 03:09发布

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?

3条回答
乱世女痞
2楼-- · 2019-03-30 03:18

you need to set custom backgroudImage for UINavigationBar, then the shadowImage can work.

查看更多
不美不萌又怎样
3楼-- · 2019-03-30 03:24

Try this !

[[UINavigationBar appearance] setShadowImage:[UIImage imageNamed:@"navbar-iphone.png"]];
查看更多
看我几分像从前
4楼-- · 2019-03-30 03:32

The Appearance proxy should work.

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...
}

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.

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)!

ClipSubviews

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

查看更多
登录 后发表回答