Search code examples
iosswiftuiscrollview

How to prevent a child view to zoom inside scrollview


I have a scroll view and inside the scrollview I am having a content view with few subview inside this content view. My requirement is to zoom the content view but not the subview of content view.

Can anyone faced this before, or did the same. Any help would be appreciated.

Thanks in advance.


Solution

  • Scroll view just apply transform to contentView. This transform applied to all children in contentView. So you can apply inverted transform to children to negate parent transform.

    func scrollViewDidZoom(_ scrollView: UIScrollView) {
        guard let content = viewForZooming(in: scrollView) else {
            return
        }
        let t = content.transform.inverted()
        for v in content.subviews {
            v.transform = t
        }
    }