我正在尝试为我的 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
关于Swift:将 View 添加到窗口时不显示阴影,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55184230/