Category

Disabled UIToolbar appearance

Swift Language

Disabled UIToolbar appearance

A short tip on how to show that a UIToolbar is disabled.

Introduction

I wanted to disable a UIToolbar and then enable it later via an In App Purchase. So, you just loop over your bar buttons and disable them, right? Well, no.

That does indeed disable the buttons. They are not responsive if you press them. But there is no visual cue.

So, let’s go back to the button creation code. After you create the button, you can call setTitleTextAttributes on it and specify separate NSAttributedString attributes for normal or disabled.

Great!

Doesn’t work. If you know why, let me know!

Table of Contents

How to do it

So, I tried something different. How about setting the tint color?
I tried this for the disabled appearance.

It does look disabled. The white isn’t exactly what I want, but it does take effect.
Actually it takes effect too well. The toolbars for every view controller are now white. That’s how the appearance proxy on the class level works.

The fix is easy enough; just set the individual button’s tintColor.

Now about that color. Of course you can set up your own set of colors. But let’s say you want to stay with the default system color. How do you get that? Unfortunately there is no direct way to find it. You have to get it from an unaltered UIView.

Here is how I get the default (blue) color.

So, to enable the toolbar:

To disable the toolbar, I just set the alpha on the default color to something < 1.

Table of Contents

Summary

One easy way to set a disabled appearance is to set the tintColor property of the buttons.
There may be a way to set the appearance through NSAttributedString attributes, but that didn’t work for me.

Resources

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.