My question is, how can I ensure that a view's layer.mask is at the back of the UI hierarchy, or, since that is not necessarily needed to accomplish the effect I'm going for, how can I put a border around a view which has rounded top corners and squared bottom corners on both iOS 7 and iOS 8? Below is the problem I'm running into with my current solution.
In iOS 7, when I set a view's layer's mask like so:
UIBezierPath *maskPath;
maskPath = [UIBezierPath bezierPathWithRoundedRect:self.view.bounds
byRoundingCorners:(UIRectCornerTopLeft | UIRectCornerTopRight)
cornerRadii:CGSizeMake(WDFeedItemViewCornerRadius,
WDFeedItemViewCornerRadius)];
CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
maskLayer.frame = self.bounds;
maskLayer.path = maskPath.CGPath;
self.view.layer.mask = maskLayer;
The result is effectively a border around the view (the top part, by "Bruce Morgan" [which is a fake name just so you're not scared], has a border around it which is squared on the bottom and rounded at the top):
However, running the same code on iOS 8 puts the mask IN FRONT of the view, like so:
which is confirmed by viewing the UI Hierarchy (unfortunately, can't view the hierarchy for iOS 7):
I've tried creating a view behind my current view for the sole purpose of applying a mask to it, as well as setting layer.zPosition to 0 and mask.zPosition to 0, but the mask STILL appears on top of everything. What's going on? How do I fix it?
Yeaaaaah... turns out a superview was calling insertSubview:belowSubview:
, and for some reason (perhaps the 'belowSubview' argument wasn't done loading?), it just added the new view on top of all the others (in iOS 8, but not in iOS 7). Changing insertSubview:belowSubview:
to insertSubview:atIndex:
with index 0 solved the issue for me.