19

I am creating a custom layout of the popover that iOS provides. I have subclassed the UIPopoverBackgroundView and got it to draw the background for my popover correctly. My problem is now that UIPopoverController creates an inner shadow on the popover affecting the contentViewController of the popover. I want to remove this inner shadow, so only the content of my contentViewController is displayed.

This is how the popover currently looks, with a UILabel to demonstrate the effect on the contentViewController.

popover

Is there any way to remove this inner shadow?

1
  • Look kajham's answer, for iOS 6 should be the accepted answer.
    – Natan R.
    Commented Dec 4, 2012 at 17:19

6 Answers 6

25

Support for this was added in ios6.0 with the following call:

+ (BOOL)wantsDefaultContentAppearance

Link to documentation: http://developer.apple.com/library/ios/#documentation/uikit/reference/UIPopoverBackgroundView_class/Reference/Reference.html

3
  • 4
    this doesn't work for me.. I have a subclass of UIPopoverBackgroundView, overrided the method, and nothing. shadows stills there.. any help?? please
    – Frade
    Commented Sep 4, 2013 at 11:30
  • @Frade you need to override and don't call super in -(void)layoutSubviews method (I found it here: stackoverflow.com/questions/10044227/…)
    – derpoliuk
    Commented May 31, 2014 at 12:59
  • to be honest, I do not remember for what purpose I needed this.. thanks anyway! ;)
    – Frade
    Commented Jun 2, 2014 at 14:13
10

Since there is no elegant way to do this and since I do not want to rewrite the entire UIPopoverController just to do this, I've created a simple hack that removes the inner shadow on the popover by traversing the UIView structure. The hack is a category on UIPopoverController and I just put it in the files for my subclass of UIPopoverBackgroundView. So here's the code:

@interface UIPopoverController(removeInnerShadow)

- (void)removeInnerShadow;
- (void)presentPopoverWithoutInnerShadowFromRect:(CGRect)rect 
                                          inView:(UIView *)view 
                        permittedArrowDirections:(UIPopoverArrowDirection)direction 
                                        animated:(BOOL)animated;

- (void)presentPopoverWithoutInnerShadowFromBarButtonItem:(UIBarButtonItem *)item 
                                 permittedArrowDirections:(UIPopoverArrowDirection)arrowDirections 
                                                 animated:(BOOL)animated;

@end

@implementation UIPopoverController(removeInnerShadow)

- (void)presentPopoverWithoutInnerShadowFromRect:(CGRect)rect inView:(UIView *)view permittedArrowDirections:(UIPopoverArrowDirection)direction animated:(BOOL)animated 
{
    [self presentPopoverFromRect:rect inView:view permittedArrowDirections:direction animated:animated];
    [self removeInnerShadow];
}

- (void)presentPopoverWithoutInnerShadowFromBarButtonItem:(UIBarButtonItem *)item 
                                 permittedArrowDirections:(UIPopoverArrowDirection)arrowDirections 
                                                 animated:(BOOL)animated
{
    [self presentPopoverFromBarButtonItem:item permittedArrowDirections:arrowDirections animated:animated];
    [self removeInnerShadow];
}

- (void)removeInnerShadow
{
    UIWindow *window = [[[UIApplication sharedApplication] delegate] window];

    for (UIView *windowSubView in window.subviews)
    {
        if ([NSStringFromClass([windowSubView class]) isEqualToString:@"UIDimmingView"])
        {
            for (UIView *dimmingViewSubviews in windowSubView.subviews)
            {
                for (UIView *popoverSubview in dimmingViewSubviews.subviews)
                {
                    if([NSStringFromClass([popoverSubview class]) isEqualToString:@"UIView"]) 
                    {
                        for (UIView *subviewA in popoverSubview.subviews)
                        {
                            if ([NSStringFromClass([subviewA class]) isEqualToString:@"UILayoutContainerView"])
                            {
                                subviewA.layer.cornerRadius = 0;
                            }

                            for (UIView *subviewB in subviewA.subviews)
                            {
                                if ([NSStringFromClass([subviewB class]) isEqualToString:@"UIImageView"] )
                                {
                                    [subviewB removeFromSuperview];
                                }
                            }
                        }
                    }
                }
            }
        } 
    }
}

@end

When I want to display my popover I just call the presentPopoverWithoutInnerShadowFromRect: and presentPopoverWithoutInnerShadowFromBarButtonItem: methods instead of the standard ones. NOTE: Remember to #import <QuartzCore/QuartzCore.h> for the code to work

4
  • If I have a UITableView inside the popover, the corner masks appear when scrolling. Any idea if there's a way to work around this?
    – bromanko
    Commented Mar 22, 2012 at 1:33
  • You can use the "removeInnerShadow" to remove the corner masks and shadow of the popover. Try playing around with it. You can try and call "removeInnerShadow" continuously while scrolling or maybe you can look for a way to stop the scrolling from adding the corner masks again.
    – Sorig
    Commented Mar 26, 2012 at 15:42
  • Turns out also setting the background color helped: subviewA.layer.backgroundColor = [UIColor clearColor].CGColor;
    – bromanko
    Commented Mar 26, 2012 at 21:00
  • 1
    This is a fairly brittle hack. The correct answer to this question should be isaac's. Roll your own.
    – thefaj
    Commented Apr 9, 2012 at 22:43
10

I juest created my own version for the project I'm working on.

Basically you should use your own custom backgroundClass for popover and in this class you should define:

- (void)willMoveToWindow:(UIWindow *)newWindow {
    [super willMoveToWindow:newWindow];
    if ([UIPopoverBackgroundView respondsToSelector:@selector(wantsDefaultContentAppearance)]) 
        return;

    if (![[self class] wantsDefaultContentAppearance]) {
        for (UIView *view in self.superview.subviews) {
            for (UIView *subview in view.subviews) {
                if (subview.layer.cornerRadius != 0.0) {
                   subview.layer.cornerRadius = 0.0;
                    for (UIView *subsubview in subview.subviews) {
                        if (subsubview.class == [UIImageView class])
                            subsubview.hidden = YES;
                    }
                }
            }
        }
    }
}

Should be pretty much bulletproof for iOS updates.

3
  • 1
    This works fine with popOvers for views, which are not embedded in NavigationControllers. As soon as the ViewController (which is embedded in the NavigationController) is used for the popover the shadow is back again. Is there any solution to this?
    – FrankZp
    Commented Dec 21, 2012 at 17:37
  • #FrankZP Look for the modification below. Commented Dec 29, 2012 at 16:21
  • +1 nice workaround! One question: could you explain a bit why does sending a message to [self class] (which is a objc_class) work?
    – matm
    Commented Sep 27, 2013 at 13:56
3

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: enter image description here

Without UINavigationBar: enter image description here

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.

3
  • You should add a condition to avoid doing any of this in iOS 6+ and just return NO from wantsDefaultContentAppearance. Commented Feb 25, 2013 at 13:35
  • +1 for detailed diagrammatic explanation. Can you also tell how to adjust border size of the UIPopover? Commented Mar 13, 2013 at 12:14
  • Do you mean the outer border? I used a UIPopoverBackgroundView to use images to specify the look of the popover. If you mean the inner border, I just removed it. The default implementation uses a UIImageView so you'd have to replace the image if you just wanted to change the border. Commented Mar 17, 2013 at 20:29
1

I don't believe that there is an elegant/supported way to achieve that using Apple's standard UIPopover. However, you could make your own custom popover class fairly easily. There are quite a few examples of how to do so both here on SO and tutorials on the wider web (even a few ready-to-download solutions). Just put 'custom uipopover' into Google...

6
  • Yeah okay. I just thought it would be nice to be able truly customize the appearance of the UIPopover. Since Apple introduced some customization capabilities with the UIPopoverBackgroundView, I just thought it might be possible to actually change the way UIPopover looks.
    – Sorig
    Commented Jan 13, 2012 at 10:53
  • This isn't the case. Reimplementing popovers all over the place is a bad idea. Commented Nov 20, 2012 at 5:52
  • 1
    @BenLachman Care to tell us why? The question doesn't say anything about "all over the place". Rather, he describes what he needs, and asks if a UIKit object suits the need. At the time it did not. There is absolutely no reason why rolling your own element in this case is a bad idea in-and-of-itself.
    – isaac
    Commented Nov 20, 2012 at 17:51
  • @issac I can't say I agree. Generally the first response to "This won't work exactly how I want" should be "Well, in that case, why do I want to do this in a custom manner?" not "Ok, let's go spend a bunch of time rolling a custom solution that is almost certainly not as robust as Apple's". Rolling your own stuff is definitely doable, but should be a carefully thought out decision not a gut reaction. Commented Nov 21, 2012 at 17:42
  • 1
    @BenLachman Your comments are entirely subjective - time spent on recreating functionality is a perfectly reasonable concern, but its not a technical reason not to do something -- likewise using other people's poor examples as reason not to do something yourself. I would agree that there are some classes the behavior of which is probably not best re-created by those without significant experience or motivation. A UIPopover just isn't one of them.
    – isaac
    Commented Nov 22, 2012 at 23:35
0

For FrankZp

This works fine with popOvers for views, which are not embedded in NavigationControllers. As soon as the ViewController (which is embedded in the NavigationController) is used for the popover the shadow is back again. Is there any solution to this?

Here is the modification for the UINavigationController:

- (void)removeInnerShadow
{
    UIWindow *window = [[[UIApplication sharedApplication] delegate] window];

    for (UIView *windowSubView in window.subviews) {
        if ([NSStringFromClass([windowSubView class]) isEqualToString:@"UIDimmingView"] {
            for (UIView *dimmingViewSubviews in windowSubView.subviews) {
                for (UIView *popoverSubview in dimmingViewSubviews.subviews) {
                    if([NSStringFromClass([popoverSubview class]) isEqualToString:@"UIView"]) {
                        for (UIView *subviewA in popoverSubview.subviews) {
                            if ([NSStringFromClass([subviewA class]) isEqualToString:@"UILayoutContainerView"]) {
                                subviewA.layer.cornerRadius = 0;
                            }

                            for (UIView *subviewB in subviewA.subviews) {
                                if ([NSStringFromClass([subviewB class]) isEqualToString:@"UILayoutContainerView"]) {
                                    for (UIView * subviewC in subviewB.subviews) {
                                        if ([NSStringFromClass([subviewC class]) isEqualToString:@"UIImageView"] ) {
                                            [subviewC removeFromSuperview];
                                        }
                                    }
                                }

                                if ([NSStringFromClass([subviewB class]) isEqualToString:@"UIImageView"] ) {
                                    [subviewB removeFromSuperview];
                                }
                            }
                        }
                    }
                }
            }
        } 
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.