Willis
Willis

Reputation: 5336

UIBarButtonItem in navigation bar is highlighted when selected but UIBarButtonItem in toolbar is not

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.

Upvotes: 1

Views: 3609

Answers (5)

Netsu
Netsu

Reputation: 363

You should change button type from UIButtonTypeCustom to UIButtonTypeSystem.

Upvotes: 3

zevij
zevij

Reputation: 2446

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

Upvotes: 0

Earl Grey
Earl Grey

Reputation: 7466

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.

Upvotes: 3

Chirag
Chirag

Reputation: 259

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
    

Upvotes: 0

leonardo
leonardo

Reputation: 1714

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.

Upvotes: 0

Related Questions