Custom UIPopoverBackgroundView: no drop shadow

spring picture spring · Apr 6, 2012 · Viewed 8.4k times · Source

I've used this good tutorial to create a custom UIPopoverBackgroundView class.

It works well. The only problem is that I am not getting the typical UIPopoverController drop shadow and I want it. I've tried specifying it on the layer of my UIPopoverBackgroundView instance without success. My instance of UIPopoverController doesn't seem to have a public view to manipulate. Adding it to the popover content also doesn't work.

Probably really simple: how do I add a drop shadow when using a custom UIPopoverBackgroundView class?

// UIPopoverBackgroundView.m

-(id)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        _borderImageView = [[UIImageView alloc] initWithImage:[[UIImage imageNamed:@"bg-popover.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(CAP_INSET,CAP_INSET,CAP_INSET,CAP_INSET)]];

        _arrowView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"bg-popover-arrow.png"]];

        [self addSubview:_borderImageView];
        [self addSubview:_arrowView];

        self.layer.shadowOffset = CGSizeMake(50, 50);
        self.layer.shadowColor = [[UIColor blackColor] CGColor];
    }

    return self;
}

Answer

Mike Bernardo picture Mike Bernardo · Oct 2, 2012

You don't need to add your own shadows. The base UIPopoverBackgroundView will do it for you. Just make sure to call super in your layoutSubviews implementation.

EDIT: My comment applies to apps targeting iOS 6.