Custom UIPopoverBackgroundView: no drop shadow

痞子三分冷 提交于 2019-12-03 05:58:38

问题


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

回答1:


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.




回答2:


Ok, figured it out. I needed to add the drop shadow to the borderImageView, not the view of the popover instance.

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


来源:https://stackoverflow.com/questions/10044227/custom-uipopoverbackgroundview-no-drop-shadow

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!