Wrong Background Color in Accessory View on iOS7

你说的曾经没有我的故事 提交于 2019-12-12 11:32:19

问题


I have a UITableView and I have set my cell background color to RGB 244, 240, 246. I've done this by setting the background color on the table, table cell, and the content view in the table cell.

However, the accessory (in this case the checkmark) has a black background instead.

When I enable editing on the table, the delete circle on the left side also has a black background.

I cannot seem to change this background color.

I've tried doing so with the following code:

cell.editingAccessoryView = [[UIView alloc] init];
cell.editingAccessoryView.backgroundColor = [UIColor colorWithRed:244/255 green:240/255 blue:246/255 alpha:1.0];

but it has no effect.

I've tried looking for a setting within the storyboard but nothing seems to make any difference.

I did notice that if I change the table cell and content view background color to "default" the whole cell background becomes black (even though the table background color is still my custom color).

I've gone through the iOS7 Transition guide and I didn't see anything related to the UIAccessoryView. I've also searched through stackoverflow but I wasn't able to find anything matching the issue I'm having.

How can I fix this?


回答1:


In iOS 7, cells have a white background by default; in earlier versions of iOS, cells inherit the background color of the enclosing table view. If you want to change the background color of a cell, do so in the tableView:willDisplayCell:forRowAtIndexPath: method of your table view delegate.

I hope this help you




回答2:


You'll need to set the backgroundView property of the UITableViewCell, you can't simply change the editingAccessoryView, as you've probably seen. Instead do something like:

UIView *aBackgroundView = [[[UIView alloc] initWithFrame:CGRectZero] autorelease];
aBackgroundView.backgroundColor = [UIColor yourColor];
yourCell.backgroundView = aBackgroundView;

Or in

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

Use:

cell.contentView.superview.backgroundColor = [UIColor redColor];



回答3:


For table view cell, there is a property called backgroundView. You only need to change the backgroundcolor for the backgroundView. Thats it.

 cell.backgroundView.backgroundColor = [UIColor yourcolor];



回答4:


For iOS 7 place this in customised table cell code

- (void)willTransitionToState:(UITableViewCellStateMask)state
{
    [super willTransitionToState:state];
    if ((state & UITableViewCellStateShowingEditControlMask) == UITableViewCellStateShowingEditControlMask)
    {
        for (UIView *subview in self.subviews)
        {
            if ([NSStringFromClass([subview class]) isEqualToString:@"UITableViewCellScrollView"])
            {
                subview.backgroundColor  = [UIColor colorWithRed:0.69 green:0.769 blue:0.871 alpha:1];
            }
        }
    }

}




回答5:


The problem was caused by the colour object. The correct line is:

cell.editingAccessoryView.backgroundColor = [UIColor colorWithRed:244/255.0f green:240/255.0f blue:246/255.0f alpha:1.0];

My best guess is that without the .0f it was treating the numbers as ints and truncating all the values to 0 (which would give me black).



来源:https://stackoverflow.com/questions/19406149/wrong-background-color-in-accessory-view-on-ios7

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!