【问题标题】:UIView masking on iOS 10iOS 10 上的 UIView 遮罩
【发布时间】:2017-02-11 14:25:22
【问题描述】:

我有一个名为myViewUIView 子类和一个名为myViewControllerUIViewController

我将myView 添加为myViewController 中的子类,并且我还在其文件中屏蔽myView 子视图。

问题是当我屏蔽时,视图完全消失了。

有人知道为什么吗?

谢谢!

我的代码:

myViewController

override func viewDidLoad() {
     let theView = myView(frame: CGRectZero)
     theView.translatesAutoresizingMaskIntoConstraints = false
     self.view.addSubview(theView)

     theView.bottomAnchor.constraintEqualToAnchor(self.view.bottomAnchor).active = true
     theView.leftAnchor.constraintEqualToAnchor(self.view.leftAnchor).active = true
     theView.rightAnchor.constraintEqualToAnchor(self.view.rightAnchor).active = true
     theView.heightAnchor.constraintEqualToAnchor(self.view.heightAnchor, multiplier: 0.17).active = true
}

myView

override func layoutSubviews() {

        super.layoutSubviews()
        let ev = UIView(frame: self.bounds)
        ev.backgroundColor = UIColor.blueColor()
        ev.translatesAutoresizingMaskIntoConstraints = false
        self.addSubview(ev)

        ev.rightAnchor.constraintEqualToAnchor(self.rightAnchor).active = true
        ev.bottomAnchor.constraintEqualToAnchor(self.bottomAnchor).active = true
        ev.leftAnchor.constraintEqualToAnchor(self.leftAnchor).active = true
        ev.heightAnchor.constraintEqualToAnchor(self.heightAnchor, multiplier: 1.5).active = true

        let myPath: CGPathRef = PocketSVG.pathFromSVGFileNamed("CategoriesBar").takeUnretainedValue() //Creating a CGPath object using a  3rd party API
        var transform: CGAffineTransform = CGAffineTransformMakeScale(self.frame.size.width / 754.0, self.frame.size.height / 220.0) //Resizing it

        let transformedPath: CGPathRef = CGPathCreateMutableCopyByTransformingPath(myPath, &transform)! //Creating a re-sized CGPath

        let myShapeLayer = CAShapeLayer()
        myShapeLayer.path = transformedPath
        myShapeLayer.fillRule = kCAFillRuleEvenOdd

        let myMaskedView = UIView(frame: self.frame)
        myMaskedView.backgroundColor = UIColor.blackColor()
        myMaskedView.layer.mask = myShapeLayer
        ev.maskView = myMaskedView //THE PROBLEM. If I remove this line the view is visible (but it isn’t masked), but when I don’t the view just completely disappears!
}

注意:问题发生在我更新到 iOS 10 时,在 iOS 9 上一切正常。

【问题讨论】:

    标签: ios iphone swift cocoa-touch uiview


    【解决方案1】:

    'viewDidLoad()' 中的

    'self.view.layoutSubviews()' 在实施 'UIView' 之前对我有用。它与iOS10中viewcontroller生命周期的变化有关。

    【讨论】:

    • 好吧,我解决了我的问题大约一个小时,我现在的理解是你应该非常小心 self.bounds self.frame 等,因为在我的情况下,我正在操纵 UIView 掩码当它在 swift 2.3 中正常的框架变成 (0, 0, 1000, 1000) 时,我通过打印 UIView.bounds 来修复它,将它放置在不同的功能中,最后找到了一种方法让它不是 ( 0、0、1000、1000)(通过添加self.view.layoutSubviews())。我的猜测是你的问题很相似,但我不是 100% 确定。
    • self.bounds: (0.0, 0.0, 375.0, 113.5) self.frame: = (0.0, 553.5, 375.0, 113.5)
    猜你喜欢
    • 2012-12-10
    • 1970-01-01
    • 2015-03-23
    • 1970-01-01
    • 2014-12-07
    • 2014-02-09
    • 2012-07-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多