你的看法是错误的,额外的视图/层会有帮助。
你可以将带有圆角的roundedContainer
放入另一个带阴影的shadowedView
中。为了避免出现白色边角,请确保在某个地方设置背景颜色为clear
。
例如:
//superview for container with rounded corners
shadowedView.backgroundColor = UIColor.clear //this will fix your white corners issue
shadowedView.layer.shadowColor = UIColor.black.cgColor
shadowedView.layer.shadowOffset = .zero
shadowedView.layer.shadowOpacity = 0.3
shadowedView.layer.shadowRadius = 5.0
//add a container with rounded corners
let roundedView = UIView()
roundedView.frame = baseView.bounds
roundedView.layer.cornerRadius = 10
roundedView.layer.masksToBounds = true
shadowedView.addSubview(roundedView)
UIView
内部的超类 ContainerView
中。然后,我使用 UIViewController
类为这个小矩形区域设置了圆角边缘。class GraphViewController: UIViewController {
@IBOutlet var graphView: GraphViewRenderer!
override func viewDidLoad() {
graphView.layer.cornerRadius = 20.0
graphView.layer.masksToBounds = true
super.viewDidLoad()
}
}
class GraphContainerView: UIView {
func applyPlainShadow() {
layer.shadowColor = UIColor.black.cgColor
layer.shadowOffset = CGSize.zero
layer.shadowRadius = 5.0
layer.shadowOpacity = 0.7
}
override init(frame: CGRect) {
super.init(frame: frame)
applyPlainShadow()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
applyPlainShadow()
}
}
cornerRadius
应用于containerView
后看不到阴影吗? - alexburtnikcontainerView.layer.masksToBounds = true
吗? - Nathan Hitchings