Swift:阴影未显示何时将视图添加到窗口中



我试图向我的CustomView投射阴影,但没有显示。使用window?.addSubview(customView)添加此CustomView。

到目前为止实施:

//CustomView setup
lazy var customView: UIView = {
    let v = UIView()
    v.translatesAutoresizingMaskIntoConstraints = false
    v.layer.cornerRadius = 8
    v.layer.shadowColor = UIColor.darkGray.cgColor
    v.layer.shadowOffset = CGSize(width: 0, height: 10)
    v.layer.shadowOpacity = 10.5
    v.layer.shadowRadius = 15.0
    v.layer.masksToBounds = true
    return v
}()
//Adding view to window
window?.addSubview(customView)
NSLayoutConstraint.activate([
    customView.leadingAnchor.constraint(equalTo: window!.leadingAnchor),
    customView.trailingAnchor.constraint(equalTo: window!.trailingAnchor),
    customView.heightAnchor.constraint(equalTo: window!.heightAnchor, multiplier: 1),
    customView.topAnchor.constraint(equalTo: window!.safeAreaLayoutGuide.bottomAnchor, constant: -100)
        ])

我遵循了这篇文章和这篇文章的建议,但是以某种方式没有显示出添加到窗口的视图。

这是因为这一行:

v.layer.masksToBounds = true

如果您想要影子拐角圆形,我建议使用两个层,一个具有阴影和masksToBounds = false,另一个是第一个的孩子,并且有角落圆形 masksToBounds = true

最新更新