自定义UIPopoverBackgroundView:没有投影

No *_*ing 15 uipopovercontroller ios

我用这个好的教程来创建一个自定义的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;
}
Run Code Online (Sandbox Code Playgroud)

Mik*_*rdo 19

您无需添加自己的阴影.基地UIPopoverBackgroundView会为你做.只需确保在您的layoutSubviews实施中调用super .

编辑:我的评论适用于针对iOS 6的应用.

  • @chaiguy要删除默认阴影,请覆盖` - (void)layoutSubviews`并将其留空. (3认同)

No *_*ing 14

好吧,想通了.我需要添加投影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;
}
Run Code Online (Sandbox Code Playgroud)

  • 只是想我会补充说这对我有用,但是我遇到了性能问题。栅格化背景层有很大帮助:'backgroundImageView.layer.shouldRasterize = YES;' (2认同)