user5936834
user5936834

Reputation:

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.

enter image description here

After adding the border:

enter image description here

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.

Upvotes: 1

Views: 1679

Answers (3)

Adrian Sluyters
Adrian Sluyters

Reputation: 2241

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.

Upvotes: 0

ddb
ddb

Reputation: 2435

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

Upvotes: 0

Konstantin
Konstantin

Reputation: 861

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

Upvotes: 2

Related Questions