ios - 由于仅变换层而在 UIView 中发出警告

标签 ios swift uiview swift4

我想在MyCustomView周围画阴影它是这样创建的:

func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
    let header = MyCustomView()
    header.contentView.dropShadow() // tried also header.dropShadow()
    return header
}

以下是定义:

class MyCustomView: UIView {
    //...
    var contentView = UIView()

    init() {
        super.init(frame: CGRect(
            x: CGFloat(0), y: CGFloat(0),
            width: CGFloat(200), height: CGFloat(200)))

        //...        
        let marginGuide = self.layoutMarginsGuide

        contentView.addSubview(someLabel)
        contentView.addSubview(anotherLabel)

        // Background colors
        self.backgroundColor = UIColor.init(hex: "EBEBF1")
        contentView.backgroundColor = .white

        // translatesAutoresizingMaskIntoConstraints
        contentView.translatesAutoresizingMaskIntoConstraints = false

        // contentView constraints
        contentView.topAnchor.constraint(
            equalTo: marginGuide.topAnchor, constant: CGFloat(0)).isActive = true
        contentView.bottomAnchor.constraint(
            equalTo: marginGuide.bottomAnchor, constant: marginSize).isActive = true
        contentView.trailingAnchor.constraint(
            equalTo: marginGuide.trailingAnchor, constant: -marginSize).isActive = true
        contentView.leadingAnchor.constraint(
            equalTo: marginGuide.leadingAnchor, constant: CGFloat(7)).isActive = true
        }
    }

extension UIView {
    func dropShadow(scale: Bool = true) {
        self.layer.masksToBounds = false
        self.layer.shadowColor = UIColor.darkGray.cgColor
        self.layer.shadowOpacity = 0.5
        self.layer.shadowOffset = CGSize(width: 0, height: -1)
        self.layer.shadowRadius = 3

        self.layer.shadowPath = UIBezierPath(rect: self.bounds).cgPath
        self.layer.shouldRasterize = true
        self.layer.rasterizationScale = scale ? UIScreen.main.scale : 1
    }
}

我收到的不是阴影,而是警告:

<CATransformLayer: 0x60400023e780> - changing property shadowColor in transform-only layer, will have no effect <CATransformLayer: 0x60400023e780> - changing property shadowOpacity in transform-only layer, will have no effect <CATransformLayer: 0x60400023e780> - changing property shadowOffset in transform-only layer, will have no effect <CATransformLayer: 0x60400023e780> - changing property shadowPath in transform-only layer, will have no effect <CATransformLayer: 0x60400023e780> - changing property shouldRasterize in transform-only layer, will have no effect <CATransformLayer: 0x60400023e780> - changing property rasterizationScale in transform-only layer, will have no effect <CATransformLayer: 0x60400023e780> - changing property shadowPath in transform-only layer, will have no effect

你知道为什么会发生这种情况吗?

最佳答案

尝试在 MyCustomView 的layoutSubviews 方法中调用 dropShadow() 函数:

class MyCustomView: UIView {

//.. your implementation 

override func layoutSubviews() {
    super.layoutSubviews()
    contentView.dropShadow()
}

关于ios - 由于仅变换层而在 UIView 中发出警告,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48102851/

相关文章:

ios - "apple-mobile-web-app-capable"注销后站点切换到 Mobile Safari

ios - 是否可以从我的共享扩展名保存文件中获取主应用程序中的文件?

ios - coredata关系自动设置为nil

Swift:如果两个选项都为零,则提前返回?

ios - 在界面生成器中设计的自定义 View 未显示

ios - 在数组中异步添加数据

swift - 如何将此 char* 转换为 String 可选?使用 LibXL 中的 xlSheetReadStrA,检查空单元格

ios - 使用应用程序加载器上传应用程序时出错 : ERROR ITMS-90725: "SDK Version Issue. 此应用程序是使用 iOS 11.4 SDK 构建的

ios - Swift:UIView.transitionWithView 完成 block 问题

ios - UIView animateKeyframes 不起作用