UINavigationBar 和新的 iOS 5+外观 API - 如何提供两个背景图像?
我想利用新的 iOS 5 外观 API 为我的应用程序中的所有 UINavigationBar 实例提供自定义背景图像。要做到这一点,就这么简单:
[[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:@"whatever.png"] forBarMetrics:UIBarMetricsDefault];
但是,对于每个实例,我想根据 translucent
属性的值提供不同的图像,例如,
// For UINavigationBar instances where translucent returns YES:
[[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:@"whatever-translucent.png"] forBarMetrics:UIBarMetricsDefault];
// Otherwise:
[[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:@"whatever.png"] forBarMetrics:UIBarMetricsDefault];
考虑到外观 API 似乎是使用配置的类方法,这样的事情可能吗?
I want to exploit the new iOS 5 appearance API to supply custom background images to all UINavigationBar instances in my app. To do this, it's as simple as this:
[[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:@"whatever.png"] forBarMetrics:UIBarMetricsDefault];
However, for each instance, I want to provide a different image depending on the value of the translucent
property, e.g.
// For UINavigationBar instances where translucent returns YES:
[[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:@"whatever-translucent.png"] forBarMetrics:UIBarMetricsDefault];
// Otherwise:
[[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:@"whatever.png"] forBarMetrics:UIBarMetricsDefault];
Given that the appearance APIs seem to be configured using class methods, is something like this possible?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
目前,无法执行您所描述的操作 - 外观代理在您调用它时对任何特定实例一无所知。
实际上,您可能需要做的是计算出您有多少个半透明条与有多少个非半透明条。选择您拥有更多的内容并使用该内容的外观代理 - 对于其他内容,当您要使其半透明(或要求全屏布局)时,您必须设置背景图像。
同时,您能否在 http://bugreport.apple.com/ 上针对您的需求提交增强请求在问吗?这并不是一个无理的要求。谢谢!
At the moment, there's no way to do what you're describing - the appearance proxy doesn't know anything about any particular instance at the time you're calling for it.
In practical terms, what you'll probably need to do is figure out how many translucent bars you'd have v. how many non-translucent ones you had. Choose whichever you have more of and use the appearance proxy for that one - for the others, when you go to make it translucent (or ask for full-screen layout), you'll have to set the background image then.
In the meantime, could you file an enhancement request at http://bugreport.apple.com/ for what you're asking? It's not an unreasonable request. Thanks!
您可以使用类外观代理全局设置它,也可以在导航栏的实例上设置它。
我目前正在导航栏的一个实例上设置背景,它似乎正在工作。我有两个不同背景的不同导航栏。如果您在实例上设置它,您应该能够调整代码。
如果你想使用类方法进行设置,你可以为所有人设置:
我承认它是相当新的,我只是像大多数人一样弄清楚它。
You can either set it globally with the class appearance proxy or set it on an instance of a navBar.
I'm currently setting background on an instance of the nav bar and it seems to be working. I have two different navBars with different backgrounds. If you set it on an instance, you should be able to condition the code.
If you want to set using the class method, you can set for all:
I will admit though that it's pretty new and I'm just figuring it out like most folks.
这个答案可能对你没有太大帮助,但对其他人可能有帮助。如果创建子类,则可以分别指定每个子类的外观。例如,我有 UITableviewCells 和一个派生自 UITableViewCells 的自定义类。我实际上这样做是有原因的,但我发现我需要专门为这两个类调用 [[UITableViewCells外观] setFont:[...]] 。
由于您似乎想要基于一个直到运行时才知道的变量来执行此操作,因此您可能不走运!
This answer probably won't be of much help to you, but it may be to others. IF you make a subclass, you can specify the appearance for each subclass separately. For instance, I have UITableviewCells and a custom class that is derived from UITableViewCells. I actually do this for a reason, but I discovered that i need to call [[UITableViewCells appearance] setFont:[...]] for both classes specifically.
Since you seem to want to do so based upon a variable that you will not know until runtime, you are probably out of luck!
如果您知道哪些类包含半透明条,则可以这样做:
You can do it like this if you know which classes contain the translucent bars:
我无法发表评论,所以必须给出答案。罗布·惠特洛(Rob Whitlow)就此写了一篇很棒的文章。查看:http://ios-blog。 co.uk/tutorials/ios-custom-ui-series-tabbar-navbar/
I cant leave a comment so will have to be an answer. Rob Whitlow wrote a great article on this. Check it out: http://ios-blog.co.uk/tutorials/ios-custom-ui-series-tabbar-navbar/
试试这个:
Try this: