Change the selected cell background colour using UIAppearance

前端 未结 3 1465
梦谈多话
梦谈多话 2020-12-20 07:43

I need to change the selected cell background colour for all the cells in my app. As I know there is a way to use UIAppearance protocol for this purposes. Is it

3条回答
  •  温柔的废话
    2020-12-20 08:17

    As null's answer is not for selected cell backgrounds and Armands L.'s answer did not work consistently for me (selecting cells by 'user-tap' did work, but programmatical cell selection showed strange results (like sometimes the selected background was not visible, or did not fill the cell's height properly...).

    I found a custom solution that worked:

    1. Subclass UITableViewCell
    2. Initialize self.selectedBackgroundView in init and
    3. Add custom UIColor property with UI_APPEARANCE_SELECTOR for custom selected background color

    .h file:

    @property (nonatomic) UIColor* selectedCellBackgroundColor UI_APPEARANCE_SELECTOR;
    

    .m file:

    in init method(s):

    self.selectedBackgroundView = [[UIView alloc] init];

    and last but not least the setter function for the color:

    - (void) setSelectedCellBackgroundColor:(UIColor*) color {
        _selectedCellBackgroundColor = color;
        self.selectedBackgroundView.backgroundColor = color;
    }
    

提交回复
热议问题