Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/120.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 将集合视图单元格的子视图发送到背面会阻止它更改背景颜色_Ios_Uiview_Uicollectionview_Background Color_Uicollectionviewcell - Fatal编程技术网

Ios 将集合视图单元格的子视图发送到背面会阻止它更改背景颜色

Ios 将集合视图单元格的子视图发送到背面会阻止它更改背景颜色,ios,uiview,uicollectionview,background-color,uicollectionviewcell,Ios,Uiview,Uicollectionview,Background Color,Uicollectionviewcell,我有一个自定义的UICollectionViewCell,我在它的contentView中添加了一个子视图,这样它的删除按钮看起来像是悬浮在单元格的一角,但有点超出了边界(就像springboard中应用程序的删除按钮一样)。一切正常,但当我尝试更改此子视图时,insetView.backgroundColor在突出显示或选中单元格后,它不会更改 在UICollectionViewCell中 - (void) layoutSubviews { [super layoutSubviews]

我有一个自定义的
UICollectionViewCell
,我在它的
contentView
中添加了一个子视图,这样它的删除按钮看起来像是悬浮在单元格的一角,但有点超出了边界(就像springboard中应用程序的删除按钮一样)。一切正常,但当我尝试更改此子视图时,
insetView.backgroundColor
在突出显示或选中单元格后,它不会更改

UICollectionViewCell中

- (void) layoutSubviews
{
    [super layoutSubviews];

    self.insetView = [[UIView alloc] initWithFrame:CGRectInset(self.bounds, self.bounds.size.width/64, self.bounds.size.height/16)];
    self.insetView.layer.cornerRadius = 6;
    self.insetView.layer.masksToBounds = YES;
    self.insetView.backgroundColor = [UIColor colorWithRed:65/255.0 green:166/255.0 blue:42/255.0 alpha:1];
    [self.contentView addSubview:self.insetView];
    [self.contentView sendSubviewToBack:self.insetView];
    self.backgroundColor = [UIColor blackColor];
}
在采集视图控制器中

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    JamCollectionViewCell *cell = (JamCollectionViewCell*)[collectionView cellForItemAtIndexPath:indexPath];
    UIView *v = [[cell.contentView subviews] firstObject];
    v.backgroundColor = [UIColor lightGrayColor];
}
我也试过了

- (void)collectionView:(UICollectionView *)collectionView didHighlightItemAtIndexPath:(NSIndexPath *)indexPath
{
  JamCollectionViewCell *cell = (JamCollectionViewCell*)[collectionView cellForItemAtIndexPath:indexPath];   
  cell.insetView.backgroundColor = [UIColor lightGrayColor];
}
我尝试了所有的组合。E尝试按顺序获取子视图并在
didHighlightItemAtIndexPath
中更改其背景色,尝试按其属性名
单元格获取子视图。insetView
并在
didSelectItemAtIndexPath
中更改其背景色,但没有任何效果

有趣的是,如果子视图
单元格.insetView未被发送到
单元格的后面,则contentView
会对更改背景颜色做出反应。因此问题的标题


很抱歉提出了这么长的问题,感谢您的帮助。

UICollectionView
UICollectionViewCell
select
highlight
内置了状态管理,但没有可见的响应。您可以尝试将此逻辑移动到
UICollectionViewCell
子类中,您可能会发现运气更好

如果您从NIB或故事板加载代码,您可以覆盖
awakeFromNib
以创建自定义背景视图(或将其添加到故事板中,并通过
IBOutlet
将其连接到单元格)。否则,请将其添加到创建其他视图的任何位置

然后,您可以覆盖自定义子类中的
setSelected:
setHighlighted:
(记得调用super)以根据当前状态调整颜色。作为一个选择状态的实现,我已经做了很多次了,它在iOS9中仍然有效

用于海报的代码:

(void)setSelected:(BOOL)selected {
    [super setSelected:selected];
    if (selected) {
        self.insetView.backgroundColor = [UIColor lightGrayColor];
    }
    else {
        self.insetView.backgroundColor = [UIColor colorWithRed:65/255.0 green:166/255.0 blue:42/255.0 alpha:1];
    }
}

如果在将视图发送到后面之前将其设置为蓝色,您仍然可以看到它吗?是的。我做了self.insetView.backgroundColor=[UIColor blueColor]
就在[self.contentView sendSubviewToBack:self.insetView]之前在UICollectionViewCell中,它现在是蓝色而不是绿色(原始颜色)。它就像一个符咒。谢谢请为其他用户将代码添加到您的答案中<代码>-(void)setSelected:(BOOL)selected{[super-setSelected:selected];如果(selected){self.insetView.backgroundColor=[UIColor-withred:65/255.0绿色:166/255.0蓝色:42/255.0 alpha:1];}对于记录,我通常将该代码提取到一个私有方法中,并从
setSelected:
setHighlighted:
实现中调用它,然后检查
if(selected | highlighted)
,而不仅仅是
if(selected)