mchoe / SwiftSVG

A simple, performant, and lightweight SVG parser
Other
1.92k stars 229 forks source link

How to center SVG? #107

Open alexanderkhitev opened 5 years ago

alexanderkhitev commented 5 years ago

Hello! After performing the resizeToFit function, the SVG image has an empty space on the right side between itself and the containerView, please tell me how to fix it?

This is example code


        let url = Bundle.main.url(forResource: maskSVGName, withExtension: "svg")!
        let containerView = UIView()
        containerView.frame = borderFrame
        containerView.layer.zPosition = 2000

        let borderView = UIView(SVGURL: url) { (svgLayer) in
            svgLayer.lineWidth = solidBorderStyle.lineWidth
            svgLayer.strokeColor = UIColor.black.cgColor
            svgLayer.fillColor = UIColor.clear.cgColor
            svgLayer.resizeToFit(containerView.bounds)
        }

        containerView.addSubview(borderView)
        self.supernode?.view.insertSubview(containerView, at: 0)```
dhruveonmars commented 5 years ago

@alexanderkhitev what I got to work for me, was to set the frame of the svg view before adding it to the container view. So, for your code this should work; borderView.frame = containerView.frame and then containerView.addSubview(borderView)

alexanderkhitev commented 5 years ago

Hey @dhruveonmars ! I'll try it later. Thank you!