2012-04-06 84 views
14

我用这个好的tutorial来创建一个自定义的UIPopoverBackgroundView类。自定义UIPopoverBackgroundView:没有投影

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

可能很简单:如何在使用自定义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; 
} 

回答

14

好了,想通了。我需要将投影添加到borderImageView,而不是popover实例的视图。

- (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; 
} 
+0

非常感谢你为这个! – 2012-05-26 17:52:55

+1

只是想我会补充说这对我有效,但我有性能问题。栅格化背景图层非常有帮助:'backgroundImageView.layer.shouldRasterize = YES;' – Maurizio 2012-07-04 16:26:22

+0

有趣 - 我没有注意到任何明显的滞后(在iPad 2上),但很好知道。 – 2012-07-04 16:33:02

19

您不需要添加自己的阴影。基地UIPopoverBackgroundView会为你做。只要确保在您的layoutSubviews实施中打电话超级。

编辑:我的意见适用于应用定位的iOS 6

+0

+1正确。我惊讶地发现我的自定义弹出窗口在iOS 6上运行后开始放下阴影。 – Mazyod 2012-10-07 13:36:06

+0

感谢您添加更新@ iOS6 - 我在看到您的评论并在'layoutSubviews'上调用super后没有添加回iOS5下检查影子。 – 2012-10-08 05:28:32

+0

我在iOS 6和我的UIPopoverBackgroundView不显示阴影:( – 2012-12-05 08:31:45