简体   繁体   English

在UIview底部添加阴影

[英]Add shadow at bottom of UIview

Actually, I want shadow at bottom of UIView. 实际上,我希望在UIView的底部有阴影。 I had tried some code but getting shadow from top side only and I am using swift 3 currently. 我尝试了一些代码,但是仅从顶部获得阴影,并且当前正在使用swift 3。

Please follow below code : 请遵循以下代码:

        let horizontalLine = UIView()
        horizontalLine.frame = CGRect.zero
        horizontalLine.backgroundColor = .lightGray
        self.addSubview(horizontalLine)

        horizontalLine.layer.shadowColor = UIColor.gray.cgColor
        horizontalLine.layer.shadowOpacity = 0.5
        horizontalLine.layer.shadowOffset = CGSize(width: 0.0, height: 2.0)
        horizontalLine.layer.masksToBounds = false
        horizontalLine.backgroundColor = .lightGray
       horizontalLine.layer.shadowRadius = 5

Also I am using snapkit library for UI Setting: 另外我正在使用snapkit库进行UI设置:

        horizontalLine.snp.makeConstraints{ (make) in
            make.height.equalTo(5)
            make.width.equalTo(self.snp.width)
            make.left.equalTo(self.snp.left)
            make.right.equalTo(self.snp.right)
            make.bottom.equalTo(self.snp.bottom)
        }

How it looks now: 现在的样子: 在此处输入图片说明

Also, I do have collection view just down that. 另外,我确实有收集视图。 And we have one more collection view just backside of that line. 在该行的背面,我们还有一个集合视图。

Please guide me guys. 请指导我。 Thanks in advance. 提前致谢。

Based on the image you show, it looks like the "shadow" you are seeing is in the cell content of the collection view above your horizontalLine view. 根据您显示的图像,看起来您所看到的“阴影”在horizontalLine视图上方的集合视图的单元格内容中。

It also looks like the shadow on your horizontalLine view is not visible at all - because its superview is clipping it. 它看起来也好像在您的horizontalLine视图上的阴影根本不可见-因为它的超级视图正在剪切它。

Try this: 尝试这个:

    // new line
    self.clipsToBounds = false
    // rest of your code...
    let horizontalLine = UIView()

try this.. 尝试这个..

let horizontalLine = UIView()
horizontalLine.frame = CGRect(x: 150, y: 350, width: 150, height: 150)
horizontalLine.backgroundColor = .lightGray
self.view.addSubview(horizontalLine)

horizontalLine.layer.shadowColor = UIColor.red.cgColor
horizontalLine.layer.shadowOffset = CGSize(width: 0.0, height: 3.0)
horizontalLine.layer.shadowOpacity = 1.0
horizontalLine.layer.shadowRadius = 0.0
horizontalLine.layer.masksToBounds = false
horizontalLine.layer.cornerRadius = 4.0

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

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