简体   繁体   中英

Wrong Background Color in Accessory View on iOS7

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.

UIAccessoryView有黑色背景

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?

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

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];

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];

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];
            }
        }
    }

}

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).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM