简体   繁体   中英

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. I am adding a border to the UICollectionViewCell using 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]; in the class which subclasses UICollectionViewCell

I tried, [self bringSubviewToFront:self.badge]; and 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. 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]; instead of [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.

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.

Programmatically it could be something like this (in your custom 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. It appears that you're drawing both the badge and border in the same layer, hence the merge of the two images.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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