Search code examples
iosobjective-cuiappearance

Globally set background color of buttons, except accessory buttons in UITableView


I am using the below code to make all my buttons in an application the same color. However the accessory icon in the UITTableView row also has it. Is there a way to ignore it in the table view?

[[UIButton appearance] setBackgroundColor:[UIColor purpleColor]];

Solution

  • A UIView conforms to the UIAppearanceContainer protocol.

    So you should use appearanceWhenContainedIn: to distinct buttons depending on where they are contained in.

    [[UIButton appearance] setBackgroundColor:[UIColor purpleColor]];    
    [[UIButton appearanceWhenContainedIn:[UITableView class], nil] setBackgroundColor:nil];
    

    It would be best to use the UITableViewCell subclass instead of the table view.