【问题标题】:Swift: Shadow not showing when view is added to windowSwift:将视图添加到窗口时不显示阴影
【发布时间】:2019-08-06 15:06:21
【问题描述】:

我试图给我的 customView 投下阴影,但它没有显示。这个 customView 使用window?.addSubview(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)
        ])

我已听从 postpost 的建议,但不知何故,添加到窗口的视图没有显示。

【问题讨论】:

    标签: swift


    【解决方案1】:

    因为这行:

    v.layer.masksToBounds = true
    

    如果你想要阴影圆角,我建议使用两层,一层有阴影和masksToBounds = false,另一层是第一层的子层并且有圆角+ masksToBounds = true

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-05-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-05-13
      • 2016-11-10
      相关资源
      最近更新 更多