Jesse Bunch Jesse Bunch - 19 days ago 6
Objective-C Question

Subclassing UINavigationBar in Xcode

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


Answer

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.