在Xcode中子类化UINavigationBar [英] Subclassing UINavigationBar in Xcode

查看:55
本文介绍了在Xcode中子类化UINavigationBar的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

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

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: http://i.stack.imgur.com/B8YX0.png

我认为实现此目的的唯一方法是将UINavigationBar子类化,并覆盖一个允许我在绘制Alpha时对其进行设置的方法.

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.

这是我的问题:

  1. 这是正确的方法吗?
  2. 此的HIG后果是什么?
  3. 我不熟悉Objective-C中的子类化,您能否提供指导或 本教程将帮助我掌握子类化子类所需的步骤 UI元素并实施更改?
  4. 配置完子类后,可以通过更改以下内容在IB中查看它: UINavigationBar的类到我的自定义类,对吗?
  5. 是否有任何陷阱"可以通过子分类来说明, 像你这样的专家可以给我个头吗?
  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?

推荐答案

这是您不应该做的事情.为什么要更改该颜色而不更改条形的整体不透明度?它会降低可读性.有多个问题:

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:

  • 没有公共API,所以它并不简单
  • 篡改子视图很脆弱,可能会破坏并且可能会产生副作用
  • 这不是可以预见的-按下/弹出控制器时,将使用视图的alpha值并对其进行动画

如果您确实要这样做,则只有一种干净的公开方式:为每个要推送的导航项自定义标题视图.您可以随意修改该数量.因此,可以将UILabel作为子视图并将其alpha设置为0.5来设置视图.

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.

这篇关于在Xcode中子类化UINavigationBar的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆