NavigationBar setShadowImage not always working

久未见 提交于 2019-12-21 05:15:37

问题


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?


回答1:


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

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




回答2:


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




回答3:


Try this !

[[UINavigationBar appearance] setShadowImage:[UIImage imageNamed:@"navbar-iphone.png"]];


来源:https://stackoverflow.com/questions/14644641/navigationbar-setshadowimage-not-always-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!