Although I agree in principal that the proper way to handle this is to roll your own Popover, in this case it's a non-issue for newer versions of the OS. Do I really want to build and maintain my own popover implementation only to support an OS that will eventually be irrelevant? If you really want to, consider some of the free open source implementations on the web.
Personally I investigated the methods suggested here and came up with my own using this page as a starting point (thanks!). It works for both scenarios (with or without a navigation bar) and is a bit safer in my opinion.
Instead of adding a method to the UIPopoverController, I added a routine to my UIPopoverBackgroundView to look for the offending views using a RELATIVE route, rather than ABSOLUTE. In short, since the code has a direct reference to the UIPopoverBackgroundView (self), it can navigate up (superview) and then down (subviews).
The view trees look like this in both scenarios:
With UINavigationBar:
Without UINavigationBar:
The two views that we are interested in are the UILayoutView and UIImage views that are bold and underlined in each graph. We can get a reference to these starting from the UIPopoverBackgroundView using the code below (assumes ARC). I execute this from layoutSubviews
in my UIPopoverBackgroundView implementation.
// Helper method for traversing child views based solely on class types
UIView* (^__unsafe_unretained __block traverseSubviews)(UIView*, NSArray*) = ^(UIView *root, NSArray* nodeTypes) {
NSString *typeName = [nodeTypes objectAtIndex:0];
for (UIView *subView in root.subviews) {
if ([NSStringFromClass([subView class]) isEqualToString: typeName]) {
if (nodeTypes.count == 1)
return subView;
else
return traverseSubviews(subView, [nodeTypes subarrayWithRange:NSMakeRange(1, nodeTypes.count - 1)]);
}
}
return (UIView*)nil;
};
// Find the subviews of interest, taking into account there could be a navigation bar
UIView *layoutView = traverseSubviews([self superview], @[@"UIView", @"UILayoutContainerView"]);
if (traverseSubviews(layoutView, @[@"UINavigationBar"])) {
layoutView = traverseSubviews(layoutView, @[@"UILayoutContainerView"]);
}
UIView *imageView = traverseSubviews(layoutView, @[@"UIImageView"]);
// Remove the default content appearance
layoutView.layer.cornerRadius = 0;
[imageView removeFromSuperview];
I use a block here for doing the traversal of the subviews to keep the code concise. It takes a view as a starting point and an array of class names. The array of classnames is the sequence of view classes that I expect where index 0 is the parent of index 1 and index 1 is the parent of index 2, etc. It returns the view represented by the last item in the array.