1

I have a simple UITableViewController with a NavigationController as the root view controller. I have added an "Options" UIBarButtonItem to the navigation bar and a "Start" UIBarButtonItem to the toolbar, shown below:

enter image description here

. The issue that I am experiencing is that the "Options" button will become highlighted when pressed, like a regular UIButton would, but the "Start" button on the toolbar does not. This is very inconvenient, as it makes it very hard for the user to know if they actually pressed the button or not. This behavior is shown below:

Options Button Not Pressed:

enter image description here

Options Button Pressed:

enter image description here

Start Button Not Pressed:

enter image description here

Start Button Pressed:

enter image description here

I can't figure out how to fix this behavior. I did verify that the "Start" button actually works, so the highlighting issue is not because the button is not working. Also, interestingly enough, the "Start" button does become highlighted when it is long pressed.

Community
  • 1
  • 1
Willis
  • 5,308
  • 3
  • 32
  • 61
  • Take a look at this SO question http://stackoverflow.com/questions/8267758/programmatically-highlight-uibarbuttonitem – MendyK Feb 12 '15 at 23:31
  • While that is a valuable resource, it doesn't really address the root cause of the issue. I would like to know why the `UIBarButtonItem` highlights automatically in the navigation bar but not in the toolbar. – Willis Feb 12 '15 at 23:37
  • 2
    Hi, I've tried creating issue you are facing. I've added toolbar and button in navigation bar and bot are getting highlighted when button is tapped. Can you please tell me how you have added button and what property you have set of start button. – Dhaivat Vyas Feb 15 '15 at 04:24
  • Can you add some code snippet please ? I have tried and button is highlighted. – hsafarya Feb 21 '15 at 16:45
  • Could you send a sample ? I just tried a test project, like your configuration (NavigationController -> TableVC with top and bottom bars... and all the buttons have the same selection effect that you are looking for (?) so not possible to reproduce your bug without some more info... – JBA Feb 21 '15 at 22:21

5 Answers5

3

You simply misconfigured the UIbarButtonItem/UIButton in Interface Builder's Attributes Inspector.

There is no point to waste time investigating such a trivial issue, no matter how puzzling it may seem. Just delete that Start button and drop a new one into the toolbar again. There is no reason for it to behave differently that the options button above.

Earl Grey
  • 7,426
  • 6
  • 39
  • 59
  • Just an addendum - I have a feeling that the Options bar button item is of type 'Done' rather than 'Custom' which could be why this isn't happening for the 'Start' button. – max_ Feb 22 '15 at 00:50
  • That would be fine, but the nice animated behaviour is the default one for barbuttonitems, for toolbar button items and also for uibuttons. Only when you start tinkering with them ,you eventually loose the fade in fade out animation. – Earl Grey Feb 22 '15 at 11:41
3

You should change button type from UIButtonTypeCustom to UIButtonTypeSystem.

Netsu
  • 355
  • 4
  • 13
0

Make sure your button gets initialised with the .Default state in IB.

Then make sure the .Highlighted state Text Color is different than the .Default state Text Color.

Enable: Shows Touch On Highlight for tests.

leonardo
  • 1,686
  • 15
  • 15
0

There isn't a built-in way, but I can think of a few custom approaches:

  • Bind the button to a target method that toggles whatever the button is meant to toggle, and then changes the button's image property accordingly. You can use 2 images one for the selected state one for the default.

  • Create your own subclass of UIBarButtonItem that looks something like this:

        @interface CustomBarButtonItem : UIBarButtonItem {
            BOOL _state;
            UIImage * selectedImage;
            UIImage * defaultImage;
        }
        - (BOOL)toggleState;
        @property (nonatomic, retain) UIImage * selectedImage;
        @property (nonatomic, retain) UIImage * defaultImage;
        @end
    
    @implementation CustomBarButtonItem
    - (BOOL)toggleState {
        if (_state) {
            // Switch to Off state
            self.image = defaultImage;
        }
        else {
            // Switch to On state
            self.image = selectedImage;
        }
        return _state = !_state;
    }
    @end
    
Chirag
  • 259
  • 1
  • 8
0

is the toolbar set to translucent? I suspect your nav bar is and tool bar is not.

zevij
  • 2,416
  • 1
  • 23
  • 32