【发布时间】: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