Subclassing UINavigationBar in Xcode - iphone

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:
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 the UINavigationBar 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?

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.

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.

Related

when to split view and controller

i often do not split view and controller related things correctly.
should i always subclass a uiview if i want custom uibuttons and backgrounds in my app and add style related stuff in my view or should i handle this in my controller? or probably subclass a uibutton?
what about alignings? when should i subclass a uiview, add buttons to it and align then or handle this in my controller?
thanks for your hints!
please leave some comments if something is unclear.
Should i always subclass a uiview if I want custom uibuttons and
backgrounds in my app and add style related stuff in my view
If your need are basic, you coan just insert wanted elements as subviews of your view without having to subclass anything. Buttons and views have enough properties to handle those simple needs. But everything depends on what are those needs.
Should i handle this in my controller? or probably subclass a
uibutton?
Subclassing UIButton for UIButton behaviour is not a bad idea :-) But as said before, what do you need as special behaviour ? A special image : there is a property for that. A special reaction on events, manage it into the controller on IBActions. A UIButton with UFO behaviours, ok, let's subclass it.
What about alignings? when should i subclass a uiview, add buttons to
it and align then or handle this in my controller?
Hmmm... I guess never. This can (should) be done in the controller. The controller controls the whole UIItems. So if you want to align one item regarding another, do it into the controller. If you want to align special graphics or text that is displayed into the button, sublass it and manage that in the drawRect method.

Approach to make a custom tabBar

What should be the best way to create a effect like this and the handling of navigation controllers and view controllers ... what to do if I don't want to re-size each subsequent view in viewcontorller and things appear as if it is a tabBar
I would recommend using an UIImageView for the blue background, then 5 UIButtons of custom type with PNG images for the actual buttons.
Subclass UIView and put all the code to set up the background and buttons in the init function. That way you can easily place your custom TabBar wherever you like.
The individual buttons also allow you to easily animate them for transitions if you want.
Update to reflect updated question:
If you want the actual UITabBar functionality, things become much more complex.
You have three basic options:
a) Implement the functionality you need from scratch in your new class
b) Subclass UITabBar and try to override the drawing code with the code above
c) Take a look at already existing alternative implementations of UITabBar and base your new class on one of them. This will probably be the easiest solution.

Updatable, custom view on a UIToolbar

I want to make a small area to present some information in the middle of a UIToolbar and was wondering what the best way to do this is.
I need to show some text and a graphic, both of which need to be updated (around every 3 seconds) as new information arrives. The graphic is similar to the iPhone signal strength indicator, so it can be either custom drawn or selected from one of 3 graphics (low, medium, high strength).
I'll probably use initWithCustomView: to create a UIBarButtonItem, although I would like the view to be clickable (to allow the user to change the information shown).
What's the best way to make that view? Can I use a NIB, or do I need to do custom drawing in the view? What's the best way to update the buttons? I'm assuming that I'll have to remake the toolbarItems array each time and set it when the information changes. Is there a cleaner way to do this? Thanks.
Using initWithCustomView: sounds like a good way to go. You can create your custom view any way you want: with a NIB, by drawing it, even using images. It can also have its own subviews. It can be any object that inherits from UIView. (So, if you wanted, you could even make it actionable by using a UIButton, a custom UIControl, or a custom UIView with a gesture recognizer attached.)
You shouldn't have to remake toolbarItems (or, for that matter, do anything with it after you've added all your button items) if you just keep a pointer to your custom view UIBarButtonItem. It could be an instance variable. Then, to update the custom view, you could access it as you would any other view. I've never actually tried this, but I can't see any problem with doing it.
You sound like you had it mostly figured out. Hope this is helpful.
I needed the same solution and was hoping for some code examples from you. So I ended up doing it all in IB and the steps are here as follows:
Create UItoolbar in IB with no Items. (A Bar Button Item will be added again once you add the UIView)
Add UIView as subview of UIToolbar
Add UILabels to subview of UIView that is already a subview of the UIToolbar.
Create IBOutlets from UIToolbar, UIView and each UILabel and then you can reference the labels in your app.
I did set the backgrounds to clearColor so the text appears on top of UIToolbar without any box or borders.
And the text updates dynamically which was the desired outcome.
Hope this helps someone as this has been eluding me for a while.

Really cool way to create custom UITabBar for iPhone app?

I am doing a lot of researching lately about how to get a different looking with nice effects UITabBar on my iPhone app, but unfortunately I am only finding things on how to replace background color etc.
Well, I've checked out this app called Momento which is pretty cool and presents a very slick tabBar:
So there are a couple of elements here I would like to ask you guys if you could help me by giving me the right directions on how to get a similar effect :)
Arrow above items: as you can see this app has this animated arrow that runs above the selected item with a very smooth animation.
Selected Stated of the item's image is not that blue-ish default one neither the default state which displays in a different shade of brown and gray version.
nice Items separators with beveled vertical lines.
different background image for the tabBar
different height for the tabBar
At this point after some research I am able to set the height and background image by subclassing UITabBarController but I'm still not sure on how to accomplish the other items specially the first one related to the nice arrow effect.
How do I do this? Please clarify what can or can't be done by subclassing the UITabBarController and specially if can be done in Interface Builder.
There's a project on github called BCTabBarController that aims to mimic the tab bar used in Twitter for iPhone. It's got some of the things you're looking for, and should give a great starting point.
Both of these are good answers, but both libraries have problems: BCTabBarController doesn't know how to create the "blue" highlighted version of a tab bar icon; and iDevRecipies doesn't send events to child viewcontrollers nor resize the navigation bar on rotate.
Be warned: custom nav bars are a lot of trial-and-error debugging (as I have found).
Simply use a UIView with TabBar width and height.Add custom background image and custom buttons on the view.Set the fileowner of the view as AppDelegate.Now you can simply connect the IBActions with the buttons.The Custom view can be placed over the tabbar by addSubView to the TabBar controller's view.You can switch between viewcontrollers by using the setSelectedIndex method of tableviewcontroller in the button action.

More than 1 button on the left side of a NavigationBar

Is it possible?
I have a UINavigationBar that I'd like to have an 'edit' button next to the 'back' button. From what I've read you can only have one or the other, which makes no sense as they are separate properties of the navigationItem object(backBarButtonItem and leftBarButtonItem).
I'm assuming you have to somehow insert a custom UIView into the UINavigationBar. I'm looking into this option and if no better solution is given then I'll outline this method.
The short answer is:
Yes, you have to add your own UIButton views (or other UIControl subclasses) to the navigation controller, yourself. So, ignore the custom *ButtonItem properties and roll it yourself.
A little more involved answer is:
Apple makes it very easy to follow their HIG guidelines, but you're on your own if you want to break them or customize. In this case, only one button is allowed, because the actual hit region is bigger than the size of the displayed button--much easier to hit from a usability standpoint.
Extraneous:
btw, there is a subtle distinction between left/rightBarButtonItem and backBarButtonItem. left/right is specified on the current UIViewController. However back is specified by the previous UIViewController.
Using a custom view is indeed your only option here. The UINavigationBar is not terribly flexible. What you will need to do is create a UIView that contains UIButtons. Be sure you realize that if you use a custom view, you gain none of the automatic behaviors of the backBarButtonItem; you have to create your own IBActions, and the graphics for the buttons as well.
On a related note, I'm not sure this is the best idea. It's usually wise to stick to Apple's UI guidelines; is there no where else you can put the edit button? The right side of the bar, for example?
While #Kelvin's answer works for pre iOS 5, if you're using iOS 5+ there is now a leftBarButtonItems array property. Note that you also must set leftItemsSupplementBackButton to true.