HomeiOS Developmentios - View Animation / Transaction doesn't work when embedded in UIViewControllerRepresentable

ios – View Animation / Transaction doesn’t work when embedded in UIViewControllerRepresentable


I’ve created a UIViewControllerRepresentable internet hosting a UIScrollView to create a customized model of the SwiftUI Scroll view.

When embedding views inside this container, their look transitions / animation don’t work any extra. When utilizing the identical views exterior the container, every thing works as anticipated:

enter image description here

Code:

struct Demo: View {
    class ViewModel: ObservableObject {
        @Printed var option1: Bool = false
        @Printed var option3: Bool = false
    }
    
    @StateObject personal var viewModel = ViewModel()
    
    @State personal var option2: Bool = false
    @State personal var option4: Bool = false

    var physique: some View {
        VStack {
            VStack {
                Textual content("In Scroll Container")
                
                UIScrollContainer {
                    VStack(spacing: 0) {
                        VStack {
                            Toggle("ViewModel Choice 1", isOn: $viewModel.option1.animation())
                            
                            if viewModel.option1 {
                                Textual content("Choice 1 is ON")
                                    .padding(.vertical, 20)
                            }
                        }
                        .padding()
                        .background(.black.opacity(0.1))
                        
                        VStack {
                            Toggle("Choice 2", isOn: $option2.animation())
                            
                            if option2 {
                                Textual content("Choice 2 is ON")
                                    .padding(.vertical, 20)
                            }
                        }
                        .padding()
                        .background(.black.opacity(0.2))
                    }
                }
            }
            .background(.pink.opacity(0.2))
            .body(maxHeight: .infinity)
            
            VStack(spacing: 0) {
                Textual content("With out Container")
                
                VStack {
                    Toggle("ViewModel Choice 3", isOn: $viewModel.option3.animation())
                    
                    if viewModel.option3 {
                        Textual content("Choice 3 is ON")
                            .padding(.vertical, 20)
                    }
                }
                .padding()
                .background(.black.opacity(0.1))
                
                VStack {
                    Toggle("Choice 4", isOn: $option4.animation())
                    
                    if option4 {
                        Textual content("Choice 4 is ON")
                            .padding(.vertical, 20)
                    }
                }
                .padding()
                .background(.black.opacity(0.2))
                
                Spacer()
            }
            .background(.yellow.opacity(0.2))
            .body(maxHeight: .infinity)
            
        }
    }
}



struct UIScrollContainer: UIViewControllerRepresentable {
    let content material: Content material
    
    init(@ViewBuilder content material: () -> Content material) {
        self.content material = content material()
    }
    
    
    func makeUIViewController(context: Context) -> UIViewController {
        let scrollViewVC = UIViewController()
        scrollViewVC.view.backgroundColor = .clear
        
        let scrollView = UIScrollView()
        scrollView.backgroundColor = .clear
        
        let contentVC = UIHostingController(rootView: self.content material)
        contentVC.sizingOptions = .intrinsicContentSize
        contentVC.view.backgroundColor = .clear
        contentVC.safeAreaRegions = []
                
        context.coordinator.contentVC = contentVC
        context.coordinator.scrollView = scrollView
        scrollView.delegate = context.coordinator
        
        scrollView.translatesAutoresizingMaskIntoConstraints = false
        scrollViewVC.view.addSubview(scrollView)
        
        NSLayoutConstraint.activate([
            scrollView.topAnchor.constraint(equalTo: scrollViewVC.view.topAnchor),
            scrollView.bottomAnchor.constraint(equalTo: scrollViewVC.view.bottomAnchor),
            scrollView.leadingAnchor.constraint(equalTo: scrollViewVC.view.leadingAnchor),
            scrollView.trailingAnchor.constraint(equalTo: scrollViewVC.view.trailingAnchor)
        ])
        
        
        contentVC.willMove(toParent: scrollViewVC)
        scrollViewVC.addChild(contentVC)
        
        contentVC.view.translatesAutoresizingMaskIntoConstraints = false
        scrollView.addSubview(contentVC.view)
        NSLayoutConstraint.activate([
            contentVC.view.topAnchor.constraint(equalTo: scrollView.contentLayoutGuide.topAnchor),
            contentVC.view.bottomAnchor.constraint(equalTo: scrollView.contentLayoutGuide.bottomAnchor),
            contentVC.view.leadingAnchor.constraint(equalTo: scrollView.contentLayoutGuide.leadingAnchor),
            contentVC.view.trailingAnchor.constraint(equalTo: scrollView.contentLayoutGuide.trailingAnchor),

            contentVC.view.widthAnchor.constraint(equalTo: scrollView.frameLayoutGuide.widthAnchor)
        ])
        
        contentVC.didMove(toParent: scrollViewVC)
        
        return scrollViewVC
    }
    
    func updateUIViewController(_ uiViewController: UIViewController, context: Context) {
        withTransaction(context.transaction) {
            context.coordinator.contentVC?.rootView = content material
        }
    }
    
    
    func makeCoordinator() -> Coordinator {
        return Coordinator()
    }
    
    class Coordinator: NSObject, UIScrollViewDelegate {
        var contentVC: UIHostingController?
        var scrollView: UIScrollView?
    }
} 

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments