Swift 3 xcode storyboard,给UIView添加box-shadow,就像CSS样式的box-shadow

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/42244040/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 09:47:29  来源:igfitidea点击:

Swift 3 xcode storyboard, add box-shadow to UIView, like a CSS style box-shadow

swiftxcodeswift3storyboard

提问by Thomas Charlesworth

As you can see, I would like to add a shadow around the edges of each UIView in the cells as white on gray hard to see it's borders clearly.

正如您所看到的,我想在单元格中每个 UIView 的边缘周围添加一个阴影,因为灰底白字很难清楚地看到它的边界。

enter image description here

在此处输入图片说明

回答by Bhautik Ziniya

Set your view's shadow properties to add a shadow.

设置视图的阴影属性以添加阴影。

SWIFT 3

斯威夫特 3

YourView.layer.shadowOpacity = 0.7
YourView.layer.shadowOffset = CGSize(width: 3, height: 3)
YourView.layer.shadowRadius = 15.0
YourView.layer.shadowColor = UIColor.darkGray.cgColor

NOTE:replace YourView with the view you want shadow.

注意:用你想要阴影的视图替换 YourView。

回答by Pineapple3D

here is my approach to this:

这是我的方法:

You create a new class called ShadowView.

您创建了一个名为 的新类ShadowView

import UIKit

@IBDesignable
class ShadowView: UIView {
    //Shadow
    @IBInspectable var shadowColor: UIColor = UIColor.black {
        didSet {
            self.updateView()
        }
    }
    @IBInspectable var shadowOpacity: Float = 0.5 {
        didSet {
            self.updateView()
        }
    }
    @IBInspectable var shadowOffset: CGSize = CGSize(width: 3, height: 3) {
        didSet {
            self.updateView()
        }
    }
    @IBInspectable var shadowRadius: CGFloat = 15.0 {
        didSet {
            self.updateView()
        }
    }

    //Apply params
    func updateView() {        
        self.layer.shadowColor = self.shadowColor.cgColor
        self.layer.shadowOpacity = self.shadowOpacity
        self.layer.shadowOffset = self.shadowOffset
        self.layer.shadowRadius = self.shadowRadius
    }
}

Then you can apply this class for you view in the storyboard like:

然后你可以应用这个类在故事板中查看,如:

storyboard 1

故事板 1

Now you can edit the values in the attributes inspector:

现在您可以在属性检查器中编辑值:

storyboard 2

故事板 2

回答by Chanchal Warde

You can select opacity as you want.

您可以根据需要选择不透明度。

override func awakeFromNib() 
{
        super.awakeFromNib()

        self.viewContainer.layer.shadowOffset =  CGSize(width: 0, height: 1)   // CGSizeMake(0, 1)
        self.viewContainer.layer.shadowColor = UIColor.black.cgColor
        self.viewContainer.layer.shadowRadius = 1.5
        self.viewContainer.layer.shadowOpacity = 0.65
        self.viewContainer.layer.cornerRadius = 1
        self.viewContainer.clipsToBounds = true
        self.viewContainer.layer.masksToBounds = false       
        self.layer.masksToBounds = false
    }