首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >由于仅变换图层导致的UIView中的警告

由于仅变换图层导致的UIView中的警告
EN

Stack Overflow用户
提问于 2018-01-05 04:11:03
回答 1查看 1.6K关注 0票数 2

我想在以这种方式创建的MyCustomView周围绘制阴影:

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

下面是定义:

代码语言:javascript
复制
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

你知道为什么会发生这样的事情吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-01-05 05:06:52

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

代码语言:javascript
复制
class MyCustomView: UIView {

//.. your implementation 

override func layoutSubviews() {
    super.layoutSubviews()
    contentView.dropShadow()
}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/48102851

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档