简体   繁体   English

iOS7上附件视图中的背景颜色错误

[英]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. 我有一个UITableView,我已经将我的单元格背景颜色设置为RGB 244,240,246。我通过在表格单元格中设置表格,表格单元格和内容视图的背景颜色来完成此操作。

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. 我已经浏览了iOS7 Transition指南,但没有看到任何与UIAccessoryView相关的内容。 I've also searched through stackoverflow but I wasn't able to find anything matching the issue I'm having. 我也搜索了stackoverflow,但我找不到任何匹配我遇到的问题。

How can I fix this? 我怎样才能解决这个问题?

In iOS 7, cells have a white background by default; 在iOS 7中,单元格默认为白色背景; in earlier versions of iOS, cells inherit the background color of the enclosing table view. 在早期版本的iOS中,单元格继承封闭表视图的背景颜色。 If you want to change the background color of a cell, do so in the tableView:willDisplayCell:forRowAtIndexPath: method of your table view delegate. 如果要更改单元格的背景颜色,请在表视图委托的tableView:willDisplayCell:forRowAtIndexPath:方法中执行此操作。

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. 您需要设置UITableViewCell的backgroundView属性,您不能简单地更改editingAccessoryView ,就像您可能已经看到的那样。 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 . 对于表视图单元格,有一个名为backgroundView的属性。 You only need to change the backgroundcolor for the backgroundView. 您只需要更改backgroundView的backgroundcolor。 Thats it. 而已。

 cell.backgroundView.backgroundColor = [UIColor yourcolor];

For iOS 7 place this in customised table cell code 对于iOS 7,将其放在自定义表格单元格代码中

- (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). 我最好的猜测是没有.0f它将数字视为整数并将所有值都截断为0(这会让我变黑)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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