Search code examples
iosios8calayermask

iOS 8: setting view.layer.mask covers view, mask is top of UI hierarchy


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):

iOS 7 with layer.mask

However, running the same code on iOS 8 puts the mask IN FRONT of the view, like so:

iOS 8 with layer.mask

which is confirmed by viewing the UI Hierarchy (unfortunately, can't view the hierarchy for iOS 7):

UI Hierarchy

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?


Solution

  • 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.