【发布时间】: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