简体   繁体   English

阴影 UIview 和 clipsToBounds

[英]Shadow UIview and clipsToBounds

I would like to set the shadow to my container UIView.我想为我的容器 UIView 设置阴影。 I use this code to make it:我使用此代码来制作它:

- (id)initWithCoder:(NSCoder*)aDecoder
{
    self = [super initWithCoder:aDecoder];
    if (self) {

        //-> drop shadow
        [self.layer setShadowColor:[UIColor blackColor].CGColor];
        [self.layer setShadowOpacity:0.6];
        [self.layer setShadowRadius:2.0];
        [self.layer setShadowOffset:CGSizeMake(2.0, 2.0)];
    }

    return self;
}

This works well.这很好用。 But, when I use _containerView.clipsToBounds = YES;但是,当我使用_containerView.clipsToBounds = YES; on this container UIView, I can't see my shadow.在这个容器 UIView 上,我看不到我的影子。 Why?为什么?

the clipsToBounds also clips your shadow. clipsToBounds还会剪辑您的阴影。 In order to prevent this you can add _containerView.layer.masksToBounds = NO which disables clipping of sublayers (see more here ).为了防止这种情况,您可以添加_containerView.layer.masksToBounds = NO以禁用子图层的剪辑(请在此处查看更多信息)。

If you have to use clipsToBounds = true because you don't want subviews to exceed your view's border, but at the same time you need a shadow on your view, I recommend adding an extra view behind your view and set the shadow properties on the extra view.如果您必须使用clipsToBounds = true因为您不希望子视图超出视图的边框,但同时您需要在视图上添加阴影,我建议在视图后面添加一个额外的视图并在视图上设置阴影属性额外的看法。

//run this in viewDidLoad() or your initialisation code
private func setupShadowContainer() {
    let containerShadow = UIView()
    parentView.addSubview(containerShadow)
    containerShadow.dropShadow()
    //using SnapKit here, you can use NSLayoutConstraint in a similar way to constraint the containerShadow behind your View
    containerShadow.snp.makeConstraints { (make) in
        make.edges.equalTo(yourView.snp.edges)
    }
}

//dropShadow method
extension UIView {
    func dropShadow() {
        self.translatesAutoresizingMaskIntoConstraints = false
        self.layer.shadowRadius = 3
        self.layer.shadowColor = UIColor.black.cgColor
        self.layer.shadowOffset = CGSize(width: 1.0, height: 1.0)
        self.layer.shadowOpacity = 0.5
        self.layer.masksToBounds = false
    }
}

Here is the UIView extension.这是 UIView 扩展。

extension UIView {
    func addShadowAndRoundCorner(cornerRadius : CGFloat) {
        self.layer.shadowOffset = .zero
        self.layer.shadowOpacity = 0.5
        self.layer.shadowRadius = 3
        self.layer.shadowColor = UIColor.black.cgColor
        self.layer.masksToBounds = false
        self.layer.cornerRadius = cornerRadius
    }
}

Call this function after creation of view as follows:创建视图后调用此函数,如下所示:

let roundView = UIView()
roundView.frame = CGRect.init(x: 0, y: 0, width: 100, height: 100)
roundView.addShadowAndRoundCorner(cornerRadius: 100/2)
- (void)layoutSubviews
{
    [super layoutSubviews];

    CAGradientLayer *l = [CAGradientLayer layer];
    l.frame = self.bounds;
    l.colors = [NSArray arrayWithObjects:(id)[UIColor clearColor].CGColor, (id)[UIColor whiteColor].CGColor, (id)[UIColor whiteColor].CGColor,(id)[UIColor whiteColor].CGColor,(id)[UIColor whiteColor].CGColor,(id)[UIColor whiteColor].CGColor, (id)[UIColor clearColor].CGColor,(id)[UIColor clearColor].CGColor, nil];
    l.startPoint = CGPointMake(0.0f, 1.0f);
    l.endPoint = CGPointMake(1.0f, 1.0f);
    self.layer.mask = l;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM