Different cell bg color depending on iOS version (4.0 to 5.0)

后端 未结 6 1810
深忆病人
深忆病人 2021-01-02 07:51

I have a custom grouped UITableViewCell, with a couple of UILabels on it. Since the UITableViewCell background color used to be pure white, it matched the UILabels\' default

相关标签:
6条回答
  • 2021-01-02 08:23

    You can call:

     [[UIDevice currentDevice] systemVersion]
    

    Although that is discouraged for a lot of reasons, and I'm not sure it's the best way to solve your problem. In principle, you should be able to deploy view code and get the consistent results you want by some other means.

    If you do actually want to compare device versions, you'd probably set a property and check the device's OS when the view controller loads, as opposed to within your cellForRowAtIndexPath...

    0 讨论(0)
  • 2021-01-02 08:25

    its use UIColor.systemBackground but its available for IOS 13 Only so use it likes this :

    if #available(iOS 13.0, *) {
    
      cell.backgroundColor = UIColor.systemBackground
    
    } else {
    
      cell.backgroundColor = .white
    
    }
    
    0 讨论(0)
  • 2021-01-02 08:26

    Nothing wrong with gcamp's answer, but if you'd prefer to keep the background white on both iOS4 and iOS5, then just do this:

    - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
        [cell setBackgroundColor:[UIColor whiteColor]];
    }
    
    0 讨论(0)
  • 2021-01-02 08:32

    I saw that problem in my custom UITableViewCell in a grouped TableView I just set the label background color to clear and that fixed it for me and works in iOS4 and iOS5. I set this in the custom cell code where the UILabel gets created as below:

        [nameLabel setBackgroundColor:[UIColor clearColor]];
    

    After that problem gone.

    0 讨论(0)
  • 2021-01-02 08:32

    What I did was set the background color of the cell to [UIColor whiteColor] in willdisplaycell...

    That way I control how things look.

    0 讨论(0)
  • 2021-01-02 08:38

    In the delegate method tableView:willDisplayCell:, the UITableViewCell will have the background colour set to white or, in iOS 5, greyish.

    You can change all your the backgroundColor of all your subviews.

    - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
        for (UIView* view in cell.contentView.subviews) {
            view.backgroundColor = cell.backgroundColor;
        }
    }
    
    0 讨论(0)
提交回复
热议问题