Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set a UIButton as both Selected and Disabled

Attempting to toggle both the selected and enabled attribute on a UIButton, therefore creating 4 potential states (Selected & Disabled, Selected & Enabled, Unselected & Disabled, unselected & Enabled).

In viewDidLoad I define the the images for the button states

- (void)viewDidLoad
{
    [super viewDidLoad];

    [self.inputToolbar.contentView.leftBarButtonItem setImage:[UIImage imageNamed:BLUE_IMAGE] forState:UIControlStateNormal];
    [self.inputToolbar.contentView.leftBarButtonItem setImage:[UIImage imageNamed:GREY_IMAGE] forState:UIControlStateSelected];
}

In viewWillAppear I conditionally set the enabled attribute along with a property (we'll call self.buttonShouldBeSelected) which then sets the UIButton's selected attribute in its setter. Along with some debugging code in viewDidLoad

- (void) viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];

    // default control states
    self.inputToolbar.contentView.leftBarButtonItem.enabled = NO;

    if (self.aBoolean) {
         self.buttonShouldBeSelected = [self.aNSNumber boolValue];
    }
}

- (void)setButtonShouldBeSelected:(BOOL)buttonShouldBeSelected
{
    self.inputToolbar.contentView.leftBarButtonItem.selected = buttonShouldBeSelected;
    _buttonShouldBeSelected = buttonShouldBeSelected;
}

- (void)viewDidAppear
{
    NSLog(@"SELECTED: %u", self.inputToolbar.contentView.leftBarButtonItem.selected);
    NSLog(@"ENABLED: %u", self.inputToolbar.contentView.leftBarButtonItem.enabled);
    NSLog(@"STATE: %lu", self.inputToolbar.contentView.leftBarButtonItem.state);
}

This works in all cases aside from one, when the button is disabled and in the selected state. In this case the UI displays the BLUE_IMAGE instead of the selected state's GREY_IMAGE and the button is correctly disabled.

In this case the log results in...

SELECTED: 1
ENABLED: 0
STATE: 6

What am I doing wrong, why is it showing the image for NormalState and what does UIControlState == 6 mean?

like image 274
user1905842 Avatar asked Dec 19 '22 07:12

user1905842


2 Answers

And for Swift 3:

inputToolbar.contentView.leftBarButtonItem.setImage(UIImage(named: GREY_IMAGE), for: [.disabled, .selected])
like image 66
Morten Gustafsson Avatar answered Dec 28 '22 23:12

Morten Gustafsson


I have come in to the same problem. I used property isUserInteractionEnabled instead of isEnabled, so the button can remain status where it was before.

like image 29
responser Avatar answered Dec 29 '22 00:12

responser