在 Xcode 中子类化 UINavigationBar
我想将自定义 alpha 值应用于 UINavigationBar
的标题。 UINavigationBar
是 UINavigationController
的一部分,UINavigationController
是 UITabBarController
的一部分。
编辑:这是我使用 UIToolbar 和 UILabel 创建的图片。我想使用 UINavigationController 中的标题执行相同操作: https://i.sstatic.net/B8YX0 .png
我认为实现这一点的唯一方法是子类化 UINavigationBar
并重写允许我在绘制时设置 Alpha 的方法。
这是我的问题:
- 这是正确的方法吗?
- 这对 HIG 有何影响?
- 我是 Objective-C 子类化的新手,你能提供指导吗? 教程将帮助我掌握子类化所需的步骤 UI 元素并实施更改?
- 当我配置子类时,我可以通过更改在IB中查看它 将
UINavigationBar
的类添加到我的自定义类,对吗? - 是否有任何“陷阱”可以提出子类化 像您这样的专家可以给我一个提示吗?
I want to apply a custom alpha value to the title of the UINavigationBar
. The UINavigationBar
is part of the UINavigationController
and the UINavigationController
is part of a UITabBarController
.
Edit: Here is a picture of what I created using a UIToolbar and a UILabel. I want to do the same using the title in the UINavigationController: https://i.sstatic.net/B8YX0.png
I think the only way to accomplish this would be to subclass the UINavigationBar
and override a method that allows me to set the Alpha when it's drawn.
Here are my questions:
- Is this the correct way to do this?
- What are the HIG ramifications of this?
- I am new to subclassing in Objective-C, can you provide a guide or
tutorial that will help me grasp the steps required in subclassing a
UI Element and implementing a change? - When I get the subclass configured, I can view it in IB by changing
the class of theUINavigationBar
to my custom class, correct? - Are there any "gotchas" that can come up with sublassing that an
expert like yourself can give me a head's up on?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是你不应该做的事情。为什么您想要更改该颜色而不是条形图的整体不透明度?它降低了可读性。存在多个问题:
如果您确实想这样做,那么只有一种干净、公开的方法:为每个要推送的导航项提供一个自定义标题视图。你可以随心所欲地篡改它。因此可以设置一个带有
UILabel
的视图作为子视图,并将其 alpha 设置为 0.5。This is something you shouldn't do. Why would you want to change that color but not the overall opacity of the bar? it decreases readability. There are multiple problems:
If you really want to do it, there's only one clean, public way: Give each navigation item to be pushed a custom title view. You can tamper with that one as much as you want. So it's possible to set a view with a
UILabel
as subview and having it's alpha set to 0.5.当导航栏由导航控制器管理时,您只能更改几个属性(请参阅 文档)。其中之一是
translucent
,它可以调整 alpha,但仅限于 Apple 确定的值。When a navigation bar is being managed by a navigation controller, there are only a few properties you're allowed to change (see docs). One of them is
translucent
which adjusts the alpha, but only to an Apple-determined value.