简体   繁体   English

如何在uicollection视图中添加阴影? IOS

[英]How to add shadow to uicollection view ? ios

I need to add a bottom shadow my collection view,here is my code: 我需要在集合视图中添加一个底影,这是我的代码:

weakself!.layer.shadowPath = UIBezierPath(rect: weakself!.bounds).CGPath
weakself!.layer.masksToBounds = false
weakself!.layer.shadowOffset = CGSizeMake(0.0, 3.0);
weakself!.layer.shadowColor = UIColor.blackColor().CGColor
weakself!.layer.shadowOpacity = 0.5

the above code is work perfect with other views. 上面的代码与其他视图完美配合。 but it make the collection view to scroll above it's bounds. 但它会使集合视图滚动到其边界上方。 as you can see here in the bellow picture where the collection view scroll above the search bar: 正如您在下面的波纹图中看到的那样,其中集合视图在搜索栏上方滚动:

在此处输入图片说明

If you need to do this you should add the collection view to a plain view, don't change the collection view, add the shadow to the plain view (the collections views superview). 如果需要执行此操作,则应将集合视图添加到纯视图中,不要更改集合视图,将阴影添加到纯视图(集合视图超级视图)。

I'd really recommend not using a shadow but instead presenting the collection as a popover or at least with a full screen backing view which dimmed the background and made the collection view more obvious (and prevented taps on other things like the partially visible buttons). 我真的建议您不要使用阴影,而应将集合显示为弹出窗口或至少具有全屏背景视图,以使背景变暗并使集合视图更明显(并防止点击其他东西,例如部分可见的按钮) 。

you are using swift.But I'm well in objective c.so the concept is same.then try this. 您正在使用swift。但是我对目标c的了解很好,所以概念是相同的。然后尝试一下。

    self.youView.layer.shadowColor = [[UIColor blackColor] CGColor];
    self.youView.layer.shadowOffset = CGSizeMake(1, 1);
    self.youView.layer.shadowOpacity = 1;
    self.youView.layer.shadowRadius = 1.0;

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

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