Swift:将 View 添加到窗口时不显示阴影

标签 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)
        ])

我听从了这个 post 的建议还有这个post , 但不知何故它不会显示添加到窗口的 View 。

最佳答案

是因为这一行:

v.layer.masksToBounds = true

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

关于Swift:将 View 添加到窗口时不显示阴影,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55184230/

相关文章:

ios - ImageView 图像动画应用程序在启动时崩溃 [SWIFT]

ios - JSON创建方法

swift - 表达式隐式强制来自 'String?不打印可选内容

swift - 在 swift 中选择自定义 UICollectionViewCell 时,不会调用 UIView 类中的 drawRect

swift - 无法从方法中永久更改实例属性?

swift - 在 Swift 中将 Z 分数转换为百分位数

swift - 基于具有关联值的 Swift 枚举过滤数组 - 不提及关联值

swift - 在 Swift 的类扩展函数中使用 'self'

ios - iOS 11 的文件提供程序扩展中的项目

ios - 未调用 UITextView 委托(delegate)方法