【问题标题】:Sending a subview of a collection view cell to the back stops it from changing backgroundColor将集合视图单元格的子视图发送到后面会阻止它更改 backgroundColor
【发布时间】:2015-12-31 05:10:14
【问题描述】:

我有一个自定义的UICollectionViewCell,我正在向它的contentView 添加一个子视图,以便它的删除按钮看起来像是悬停在单元格的一角,但有点超出边界(就像删除按钮一样跳板中的应用程序)。一切正常,但是当我在单元格突出显示或选择后尝试更改此子视图 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];
}

CollectionViewController

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

并尝试了所有组合 i。 e.尝试按其顺序获取子视图并在didHighlightItemAtIndexPath 中更改其背景颜色,并尝试通过其属性名称cell.insetView 获取子视图并在didSelectItemAtIndexPath 中更改其背景颜色,但没有任何效果。

有趣的是,如果子视图 cell.insetView 发送到 cell.contentView 的后面,它确实响应改变背景颜色的方式和任何地方.因此是问题的标题。

抱歉,问题太长了,感谢您的帮助。

【问题讨论】:

  • 如果把view设置成蓝色再发到后面,还能看到吗?
  • 是的。我在 UICollectionViewCell 中的[self.contentView sendSubviewToBack:self.insetView]; 之前做了self.insetView.backgroundColor = [UIColor blueColor];,现在它是蓝色而不是绿色(原始颜色)。

标签: ios uiview uicollectionview background-color uicollectionviewcell


【解决方案1】:

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

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

然后您可以在您的自定义子类中覆盖setSelected:setHighlighted:(记得调用super)来根据当前状态调整颜色。作为选择状态的实现,我已经多次这样做了,并且它继续在 iOS 9 中工作。

适用于海报的代码:

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

【讨论】:

  • 它就像一个魅力。谢谢。请将代码添加到其他用户的答案中。 - (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]; } }
  • 为了记录,我通常将该代码提取到一个私有方法中并从setSelected:setHighlighted: 实现中调用它,然后我检查if (selected || highlighted) 而不仅仅是if (selected)
猜你喜欢
  • 2020-07-10
  • 1970-01-01
  • 2012-02-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多