noname
noname

Reputation: 127

Resize sublayers of the custom Views

I have my custom sublayer inside UICollectionView but it does not resize on orientation changed. I know that Auto-layout does not works and found that whenever I set the frame of the layout from a superview it should stick to it. Next solution whould be setNeedsLayout() but also does not work - or maybe I am placing it in the wrong place.

Here is a code which draw cross :

func crossLayer(_ view: UIView) {
        let lineColor = UIColor.rgbColor(red: 35, green: 71, blue: 75, alpha: 1.0).cgColor

        var yVerticalStart: CGFloat = 0
        var yVerticalEnd:CGFloat = 0
        var yHorizontalStart: CGFloat = 0
        var yHorizontalEnd:CGFloat = 0

        switch deviceIdiom {
        case .pad:
            yVerticalStart = 20
            yVerticalEnd = frame.height * 0.38 - 20
            yHorizontalStart = frame.height * 0.38 / 2
            yHorizontalEnd = frame.height * 0.38 / 2
        case .phone:
            yVerticalStart = 20
            yVerticalEnd = 130
            yHorizontalStart = 75
            yHorizontalEnd = 75
        default:
            break
        }

        // Vertical line.
        let verticalPath = UIBezierPath()

        // Added 20 points from top side of the view.
        verticalPath.move(to: CGPoint(x: frame.width / 2, y: yVerticalStart))
        verticalPath.addLine(to: CGPoint(x: frame.width / 2, y: yVerticalEnd))

        let verticalLayer = CAShapeLayer()
        verticalLayer.frame = view.bounds

        verticalLayer.path = verticalPath.cgPath
        verticalLayer.lineWidth = 1
        verticalLayer.strokeColor = lineColor

        addSublayer(verticalLayer)

        // Horizontal line.
        let horizontalPath = UIBezierPath()

        // Added 20 points from left side of the view.
        horizontalPath.move(to: CGPoint(x: 20, y: yHorizontalStart))
        horizontalPath.addLine(to: CGPoint(x: frame.width - 20, y: yHorizontalEnd))

        let horizontalLayer = CAShapeLayer()
        horizontalLayer.frame = view.bounds

        horizontalLayer.path = horizontalPath.cgPath
        horizontalLayer.lineWidth = 1
        horizontalLayer.strokeColor = lineColor

        addSublayer(horizontalLayer)
    }

And the call is inside custom UICollectionView inside method :

override func draw(_ rect: CGRect) {
        super.draw(rect)
        layer.crossLayer(self)
    }

And in VC - >

override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
        super.viewWillTransition(to: size, with: coordinator)
        coordinator.animate(alongsideTransition: nil, completion: {
            _ in
            UIView.animate(withDuration: 1.0, delay: 0.0, usingSpringWithDamping: 0.5, initialSpringVelocity: 0.5, options: .curveLinear, animations: {
self.MyCollectionView.layer.layoutSublayers()
                self.MyCollectionView.layer.setNeedsLayout()            })
        })
    }

Non of these solutions work. Any ideas what am I missing ?

Thank you!

Upvotes: 0

Views: 349

Answers (1)

yerpy
yerpy

Reputation: 1446

Add following lines into crossLayer function.

verticalLayer.contentsGravity = kCAGravityResize
horiontalLayer.contentsGravity = kCAGravityResize

It should solve your problem.

Cheers!

Upvotes: 1

Related Questions