自定义UIPopoverBackgroundView:没有阴影

我用这个很好的教程来创build一个自定义的UIPopoverBackgroundView类。

它运作良好。 唯一的问题是,我没有得到典型的UIPopoverController阴影,我想要的。 我已经尝试在我的UIPopoverBackgroundView实例的层上指定它,但没有成功。 我的UIPopoverController实例似乎没有公共视图来操作。 将它添加到popover内容也是行不通的。

可能很简单: 如何在使用自定义UIPopoverBackgroundView类时添加投影?

// 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; } 

你不需要添加自己的阴影。 基本的UIPopoverBackgroundView将为你做。 只要确保在layoutSubviews实现中调用super。

编辑:我的评论适用于iOS 6的应用程序。

好吧,算出来。 我需要将投影添加到borderImageView ,而不是borderImageView实例的视图。

 - (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]; _borderImageView.layer.cornerRadius = 5.0f; _borderImageView.layer.masksToBounds = NO; _borderImageView.layer.borderWidth = 1.0f; _borderImageView.layer.borderColor = [UIColor blackColor].CGColor; _borderImageView.layer.shadowColor = [UIColor blackColor].CGColor; _borderImageView.layer.shadowOpacity = 0.8; _borderImageView.layer.shadowRadius = 50; _borderImageView.layer.shadowOffset = CGSizeMake(-10.0f, 10.0f); } return self; }