Showing posts with label Objective-C. Show all posts
Showing posts with label Objective-C. Show all posts

Friday, February 17, 2012

Customizing Appearance of Disabled UIBarButtonItems

Have you ever wanted to change the appearance of a disabled UIBarButtonItem? It seems to be a fairly common issue without many good solutions - at least, until iOS 5.0, anyway. Perhaps you've tried this one:

UIBarButtonItem button = 
    new UIBarButtonItem("Untitled 1", UIBarButtonItemStyle.Plain, null);
button.TintColor = UIColor.White;
button.Enabled = false;

It all works fine, up until you disabled the button, at which point your button becomes a very faded version of whatever you had in there. Normally, that's what you want to have happen, because dimming the button is a good indication to the user that the button cannot be pressed at the current time. However, there may be situations where that isn't true...

For example, I have a toolbar with a document title in the center. It's a UIBarButtonItem, so that it can detect touches, and respond by showing a UITextField that allows the document title to be edited. The document editor has two modes: an edit mode, and a run mode. In run mode, I don't want the title to be edited, so I disable the button. However, I still want it to be nice and visible, like a title should be. I also wanted to avoid the obvious workaround of swapping out my title button for another non-functional button with the same content. It would work, but it would kind of break some of the behind-the-scenes stuff we're doing with our user interaction model, and would require me to maintain the state of two separate buttons with the title. Also, a button that is enabled has a little sparkly animation when it is touched, to let you know that it is touchable and that the system recognized that you touched it. The two-button workaround means I would still have that animation, and gives the user the impression that they can still edit the title, but it's not working right.

So, what did I do? Find out after the break.