【问题标题】:Unable to create shadows around button and UIView无法在按钮和 UIView 周围创建阴影
【发布时间】:2019-02-07 06:56:03
【问题描述】:

我在我的应用中添加了一个按钮并使用以下代码进行设置:

locationButton.layer.cornerRadius = 7
locationButton.clipsToBounds = true
locationButton.layer.shadowColor = UIColor.black.cgColor
locationButton.layer.shadowOffset = CGSize(width: 0, height: 3)
locationButton.layer.shadowRadius = 3
locationButton.layer.shadowOpacity = 1
locationButton.layer.masksToBounds = true

我用 UIView 做了同样的事情

profileView.layer.shadowColor = UIColor.black.cgColor
profileView.layer.shadowOffset = CGSize(width: 0, height: 5)
profileView.layer.shadowRadius = 10
profileView.layer.shadowOpacity = 0.5
profileView.layer.masksToBounds = true

按钮显示时完全没有阴影,而视图的阴影不是在边框周围,而是在其自身内部的元素周围。

【问题讨论】:

  • 当您设置 layer.maskToBounds = true 时,该层的任何延伸到其边界之外的子层都将被剪裁到这些边界。由于阴影存在于边界之外,因此它将被剪切到边界。您可以选择创建一个固定在父视图边缘的内部 containerView。阴影应用于父视图的图层,而圆角应用于容器视图。然后,只需将所有内容添加到 containerView 即可。

标签: ios swift button view shadow


【解决方案1】:

这是工作代码

button.layer.masksToBounds = false
button.layer.shadowColor = UIColor.red.cgColor
button.layer.shadowOffset = CGSize(width: 5, height: 5)
button.layer.shadowOpacity = 1
button.layer.borderWidth = 1

【讨论】:

    猜你喜欢
    • 2012-09-20
    • 2020-01-11
    • 1970-01-01
    • 2013-08-08
    • 1970-01-01
    • 2015-08-17
    • 1970-01-01
    • 1970-01-01
    • 2013-04-24
    相关资源
    最近更新 更多