Subview appears underneath superviews layer.border?

According to the Apple specification: It is composited above the receiver’s contents and sublayers.

So, the border will always be above of all your subviews, even if you bring your subview to the front and so on.

So I make a background view to fake the border.

E.g.:

UIView *backgroundView = [[UIView alloc] initWithFrame:CGRectMake(100, 100, 200, 200)];
backgroundView.backgroundColor = [UIColor blackColor];
backgroundView.clipsToBounds = NO;

UIView *bView = [[UIView alloc] initWithFrame:CGRectInset(backgroundView.bounds, 3, 3)];
bView.backgroundColor = [UIColor redColor];

UIView *cView = [[UIView alloc] initWithFrame:CGRectMake(-50, -50, 100, 100)];
cView.backgroundColor = [UIColor yellowColor];
[bView addSubview:cView];

[backgroundView addSubview:bView];

[self.window addSubview:backgroundView];

and the effect:

enter image description here

Leave a Comment