简体   繁体   English

将子视图置于最前面

[英]Bring subview to front

I am having a issue where I want to bring the badge notification view which i add to the UICollectionViewCell to the front. 我遇到一个问题,我想将添加到UICollectionViewCell的徽章通知视图置于最前面。 I am adding a border to the UICollectionViewCell using cell.layer.borderColor = [[UIColor redColor]CGColor]; 我正在使用cell.layer.borderColor = [[UIColor redColor]CGColor];UICollectionViewCell添加边框cell.layer.borderColor = [[UIColor redColor]CGColor]; . But when the border is added it appears on the top of the badge view. 但是,添加边框后,它将显示在徽章视图的顶部。

在此处输入图片说明

After adding the border: 添加边框后:

在此处输入图片说明

I add the badge view to the cell using, [self addSubview:self.badge]; 我使用[self addSubview:self.badge];将徽章视图添加到单元格中[self addSubview:self.badge]; in the class which subclasses UICollectionViewCell 在子类UICollectionViewCell的类中

I tried, [self bringSubviewToFront:self.badge]; 我尝试过, [self bringSubviewToFront:self.badge]; and self.badge.layer.zPosition = 1; 并且self.badge.layer.zPosition = 1; but it didn't help. 但这没有帮助。 I found these solutions on similar posts on SO, but I think i am doing something wrong. 我在SO的类似文章中找到了这些解决方案,但我认为我做错了。 I would be glad if someone could point that out. 如果有人能指出这一点,我将感到非常高兴。 Thank you. 谢谢。

EDIT I did a little change according to the suggestions, and did [self.contentView addSubview:self.badge]; 编辑我根据建议做了一些改动,并做了[self.contentView addSubview:self.badge]; instead of [self addSubview:self.badge]; 而不是[self addSubview:self.badge]; . When i debug the view and check all the layers of view, it does show the badge on top of the border, but thats not the case in simulator. 当我调试视图并检查所有视图层时,它的确在边框顶部显示了徽章,但在模拟器中并非如此。

As @iphonic said, you cannot achieve these using same view for draw border and contain badge. 正如@iphonic所说,您不能使用相同的视图作为绘制边框并包含徽章。

Try to set these view hierarchy: 尝试设置以下视图层次结构:

Cell
   |- ContentView
      |- Main View (Apply border here)
         |- All you content
      |- Badge

you can create a specific subview for your custom UICollectionViewCell 's border, so when you need to make it appear, then you can easily manage to put the badge to the front. 您可以为自定义UICollectionViewCell的边框创建一个特定的子视图,因此当需要使其出现时,可以轻松地将徽标放置在最前面。

Programmatically it could be something like this (in your custom UICollectionViewCell ) 以编程方式它可能是这样的(在您的自定义UICollectionViewCell

UIView* border = [[UIView alloc] initWithFrame:cell.frame];
border.tag = 1001;
border.layer.borderColor = [[UIColor redColor]CGColor];
[self addSubview:border];

And when you need to add and show the badge upfront 当您需要添加和显示徽章时

[self addSubview:self.badge];
[self bringSubviewToFront:[self viewWithTag:1001]];
[self bringSubviewToFront:self.badge]; 

Use a separate CALayer for the badge. 徽章使用单独的CALayer。 It appears that you're drawing both the badge and border in the same layer, hence the merge of the two images. 看来您在同一图层中同时绘制了标志和边框,因此合并了两个图像。

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

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