【问题标题】:Custom UIPopoverBackgroundView: no drop shadow自定义 UIPopoverBackgroundView:没有阴影
【发布时间】:2012-04-20 02:46:06
【问题描述】:

我用这个好的tutorial 创建了一个自定义的 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;
}

【问题讨论】:

    标签: ios uipopovercontroller


    【解决方案1】:

    您不需要添加自己的阴影。基地UIPopoverBackgroundView 将为您完成。只需确保在您的 layoutSubviews 实现中调用 super。

    编辑:我的评论适用于面向 iOS 6 的应用。

    【讨论】:

    • +1 正确。我对我的自定义弹出框在 iOS 6 上运行后开始投下阴影感到惊讶后来到这里
    • 感谢 @iOS6 添加更新 - 在看到您的评论并在 layoutSubviews 上调用 super 后,我已返回 iOS5 下检查,但未添加阴影。
    • 我在 iOS 6 中,我的 UIPopoverBackgroundView 没有显示阴影 :(
    • 我在 iOS 6 中,我的 UIPopoverBackgroundView 正在显示阴影......虽然我不希望它:)
    • @chaiguy 要删除默认阴影,请覆盖 -(void)layoutSubviews 并将其留空。
    【解决方案2】:

    好的,想通了。我需要将投影添加到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;
    }
    

    【讨论】:

    • 只是想补充一点,这对我有用,但我遇到了性能问题。光栅化背景层有很大帮助:'backgroundImageView.layer.shouldRasterize = YES;'
    • 有趣 - 我没有注意到任何明显的滞后(在 iPad 2 上),但很高兴知道。
    • 任何阅读此答案的人,按照@Maurizio 所说的去做!。当shouldRasterize 设置为YES 时,不仅性能得到了提高,而且所有由添加阴影引起的性能问题都减少了!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-04-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多