在 Xcode 中子类化 UINavigationBar

发布于 2024-09-16 21:25:04 字数 770 浏览 6 评论 0原文

我想将自定义 alpha 值应用于 UINavigationBar 的标题。 UINavigationBarUINavigationController 的一部分,UINavigationControllerUITabBarController 的一部分。

编辑:这是我使用 UIToolbar 和 UILabel 创建的图片。我想使用 UINavigationController 中的标题执行相同操作: https://i.sstatic.net/B8YX0 .png

我认为实现这一点的唯一方法是子类化 UINavigationBar 并重写允许我在绘制时设置 Alpha 的方法。

这是我的问题

  1. 这是正确的方法吗?
  2. 这对 HIG 有何影响?
  3. 我是 Objective-C 子类化的新手,你能提供指导吗? 教程将帮助我掌握子类化所需的步骤 UI 元素并实施更改?
  4. 当我配置子类时,我可以通过更改在IB中查看它 将 UINavigationBar 的类添加到我的自定义类,对吗?
  5. 是否有任何“陷阱”可以提出子类化 像您这样的专家可以给我一个提示吗?

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:

  1. Is this the correct way to do this?
  2. What are the HIG ramifications of this?
  3. 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?
  4. When I get the subclass configured, I can view it in IB by changing
    the class of the UINavigationBar to my custom class, correct?
  5. Are there any "gotchas" that can come up with sublassing that an
    expert like yourself can give me a head's up on?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

↘紸啶 2024-09-23 21:25:04

这是你不应该做的事情。为什么您想要更改该颜色而不是条形图的整体不透明度?它降低了可读性。存在多个问题:

  • 没有公共 API,因此
  • 对子视图的篡改很脆弱,可能会损坏,并且可能会产生未预见到的副作用
  • - 使用视图的 alpha 值并且 推送/弹出控制器时的动画

如果您确实想这样做,那么只有一种干净、公开的方法:为每个要推送的导航项提供一个自定义标题视图。你可以随心所欲地篡改它。因此可以设置一个带有 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:

  • there is no public API, so it's not trivial
  • tampering with the subviews is fragile, may break and may have side-effects
  • it's not foreseen to be done -- the alpha value of the view is used and animated when pushing/popping a controller

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.

淡淡的优雅 2024-09-23 21:25:04

当导航栏由导航控制器管理时,您只能更改几个属性(请参阅 文档)。其中之一是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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文