UIPopoverController Nav Bar Appearance

泪湿孤枕 提交于 2019-12-11 09:57:05

问题


I am using this code to customise my nav bar image throughout my app.

UIImage *navBarTexture = [[UIImage imageNamed:@"NavBarTexture_iPad"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 0, 0, 0)];
    [[UINavigationBar appearance] setBackgroundImage:navBarTexture forBarMetrics:UIBarMetricsDefault];
    [[UINavigationBar appearance] setBackgroundImage:navBarTexture forBarMetrics:UIBarMetricsLandscapePhone];

This works wonders. However, when using this image on the nav bar in UIPopoverControllers, it looks a bit strange. The default Apple image for it is what I want to use, how can I make it retain that original appearance?

I know I can use appearanceWhenContainedIn: however if I return nil as an image, I just get a black space.


回答1:


The first thing that crossed my mind is to simply get the default image from the navigation bar before customization. I was shocked that it worked.

UINavigationBar *appearanceProxBar = [UINavigationBar appearance];
UIImage *defaultImage = [appearanceProxBar backgroundImageForBarMetrics:UIBarMetricsDefault];
[appearanceProxBar setBackgroundImage:navBarTexture forBarMetrics:UIBarMetricsDefault];
[[UINavigationBar appearanceWhenContainedIn:[UIPopoverController class], nil] setBackgroundImage:defaultImage forBarMetrics:UIBarMetricsDefault];

Also, as you can see in the answer, the WWDC 2012 - 216 - Advanced Appearance Customization on iOS had a neat trick of casting the appearance proxy to an instance of the appropriate class so the compiler can warn of unrecognized selectors, and also code completion is more precise.



来源:https://stackoverflow.com/questions/12440250/uipopovercontroller-nav-bar-appearance

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