【问题标题】:How to add shadow to UIView that has round corners如何向具有圆角的 UIView 添加阴影
【发布时间】:2019-01-20 21:06:56
【问题描述】:

我知道已经有人问过这个问题,但我尝试了必须的答案,但它似乎对我不起作用。

所以我想要一个 UIView,它在视图的侧面和按钮周围有阴影,而不是顶部。我将如何做到这一点,因为我有圆角。

用户界面如下所示:

这是我迄今为止尝试过的(这似乎不起作用):

featureOneView.layer.cornerRadius = 10.0
        featureOneView.clipsToBounds = true


        featureOneView.layer.shadowOffset = CGSize(width: 0, height: 3)
        featureOneView.layer.shadowOpacity = 0.6
        featureOneView.layer.shadowRadius = 3.0
        featureOneView.layer.shadowColor = UIColor.gray.cgColor

【问题讨论】:

  • 删除剪辑使其工作(测试)。如果你想要右边的阴影,也可以将宽度设置为 3。

标签: swift uiview shadow


【解决方案1】:

我确实认为问题在于限制边界。如果你想让视图剪辑边界,你应该有两个视图——一个用于阴影,一个用于内容。

这对你有用吗?

// corner radius
featureOneView.layer.cornerRadius = 10

// shadow
featureOneView.layer.shadowColor = UIColor.black.cgColor
featureOneView.layer.shadowOffset = CGSize(width: 3, height: 3)
featureOneView.layer.shadowOpacity = 0.6
featureOneView.layer.shadowRadius = 3.0

提供了一个快速的谷歌搜索:https://stackoverflow.com/a/34984063/6885097

【讨论】:

  • 什么是blueView?我必须添加为子视图吗?
  • 已修复,在两个视图的示例中是一个错字。
  • 感谢您的帮助!
【解决方案2】:

我编写了一个完美运行的 IBDesingable 类

@IBDesignable
class RoundedButton: UIButton {

    @IBInspectable var cornerRadius: CGFloat = 10.0 {
        didSet {
            self.layer.cornerRadius = cornerRadius
        }
    }

    @IBInspectable var borderWidth: CGFloat = 0.0 {
        didSet {
            self.layer.borderWidth = borderWidth
        }
    }

    @IBInspectable var borderColor: UIColor = #colorLiteral(red: 1.0, green: 1.0, blue: 1.0, alpha: 1.0) {
        didSet {
            self.layer.borderColor = borderColor.cgColor
        }
    }

    @IBInspectable
    var shadowRadius: CGFloat {
        get {
            return layer.shadowRadius
        }
        set {
            layer.shadowRadius = newValue
        }
    }

    @IBInspectable
    var shadowOpacity: Float {
        get {
            return layer.shadowOpacity
        }
        set {
            layer.shadowOpacity = newValue
        }
    }

    @IBInspectable
    var shadowOffset: CGSize {
        get {
            return layer.shadowOffset
        }
        set {
            layer.shadowOffset = newValue
        }
    }

    @IBInspectable
    var shadowColor: CGColor? {
        get {
            if let color = layer.shadowColor {
                return color
            }
            return nil
        }
        set {
            if let color = newValue {
                layer.shadowColor = color
            } else {
                layer.shadowColor = nil
            }
        }
    }

    override func awakeFromNib() {
        self.setupView()
    }

    override func prepareForInterfaceBuilder() {
        super.prepareForInterfaceBuilder()
    }

    func setupView() {
        self.layer.cornerRadius = cornerRadius
        self.layer.borderWidth = borderWidth
        self.layer.borderColor = borderColor.cgColor
        self.layer.shadowColor = shadowColor ?? UIColor.gray.cgColor
        self.layer.shadowOffset = shadowOffset
        self.layer.shadowRadius = shadowRadius
        self.layer.shadowOpacity = shadowOpacity
    }


}

您可以添加框架/角半径/偏移/颜色/不透明度 希望对您有所帮助

【讨论】:

  • 感谢您的帮助!
猜你喜欢
  • 2020-07-22
  • 2013-08-26
  • 1970-01-01
  • 2014-09-15
  • 1970-01-01
  • 2012-07-11
  • 2011-06-12
相关资源
最近更新 更多