Fix: SwiftUI View Not Updating When @State Changes in UIViewControllerRepresentable

Written by

in

Quick answer: SwiftUI automatically refreshes views when @State properties change. If a UIViewControllerRepresentable doesn’t update, implement updateUIViewController(_:context:) to respond to binding changes, and ensure the bound @State is passed correctly to the representable’s initializer.

Why this happens

SwiftUI’s view update mechanism relies on property wrappers like @State, @Binding, @ObservedObject, and @StateObject. When a wrapped value changes, SwiftUI marks the view as needing a re-render and calls body again. For pure SwiftUI views, this works automatically.

The problem appears when wrapping UIKit view controllers via UIViewControllerRepresentable. The representable has two key lifecycle methods:

  • makeUIViewController(context:) — called once to create the view controller
  • updateUIViewController(_:context:) — called whenever SwiftUI detects a change in the representable’s inputs

If you only implement makeUIViewController and forget updateUIViewController, the wrapped view controller never reacts to @State or @Binding changes from the parent view. The parent view re-renders, but the representable’s updateUIViewController is either missing or not reading the latest bound values.

Another common cause: passing a @State value as a plain Int instead of a Binding<Int> to the representable. Without a binding, the representable receives a snapshot of the value at creation time and has no way to observe future changes.

How to fix it

1. Implement updateUIViewController to respond to binding changes

The most likely fix is adding updateUIViewController to your UIViewControllerRepresentable and using the bound value to drive the wrapped controller.

import SwiftUI

struct PageViewController: UIViewControllerRepresentable {
    var controllers: [UIViewController]
    @Binding var currentPage: Int

    func makeUIViewController(context: Context) -> UIPageViewController {
        let pageVC = UIPageViewController(
            transitionStyle: .scroll,
            navigationOrientation: .horizontal
        )
        pageVC.dataSource = context.coordinator
        pageVC.delegate = context.coordinator
        return pageVC
    }

    func updateUIViewController(_ pageVC: UIPageViewController, context: Context) {
        // Only animate if the page actually changed
        guard currentPage < controllers.count else { return }
        let direction: UIPageViewController.NavigationDirection = 
            currentPage > context.coordinator.previousPage ? .forward : .reverse
        pageVC.setViewControllers(
            [controllers[currentPage]],
            direction: direction,
            animated: true
        )
        context.coordinator.previousPage = currentPage
    }

    func makeCoordinator() -> Coordinator {
        Coordinator(self)
    }

    class Coordinator: NSObject, UIPageViewControllerDataSource, UIPageViewControllerDelegate {
        var parent: PageViewController
        var previousPage = 0

        init(_ parent: PageViewController) { self.parent = parent }

        func pageViewController(_ pageVC: UIPageViewController,
                                viewControllerBefore vc: UIViewController) -> UIViewController? {
            guard let index = parent.controllers.firstIndex(of: vc), index > 0 else { return nil }
            return parent.controllers[index - 1]
        }

        func pageViewController(_ pageVC: UIPageViewController,
                                viewControllerAfter vc: UIViewController) -> UIViewController? {
            guard let index = parent.controllers.firstIndex(of: vc),
                  index < parent.controllers.count - 1 else { return nil }
            return parent.controllers[index + 1]
        }

        func pageViewController(_ pageVC: UIPageViewController,
                                didFinishAnimating finished: Bool,
                                previousViewControllers: [UIViewController],
                                transitionCompleted completed: Bool) {
            if completed,
               let visible = pageVC.viewControllers?.first,
               let index = parent.controllers.firstIndex(of: visible) {
                parent.currentPage = index
            }
        }
    }
}

Key points:
– The currentPage parameter is a @Binding so changes flow both ways.
– updateUIViewController reads currentPage and tells the UIPageViewController to scroll.
– The Coordinator tracks the previous page to decide animation direction and writes back to the binding when the user swipes.

2. Pass the binding correctly from the parent view

In the parent PageView, pass $currentPage (the binding) not currentPage (the plain value).

struct PageView<Page: View>: View {
    var viewControllers: [UIHostingController<Page>]
    @State private var currentPage = 0

    init(_ views: [Page]) {
        self.viewControllers = views.map { UIHostingController(rootView: $0) }
    }

    var body: some View {
        VStack {
            PageViewController(controllers: viewControllers, currentPage: $currentPage)

            HStack { /* buttons that modify currentPage */ }
        }
    }

    func nextPage() { /* ... */ }
    func prevPage() { /* ... */ }
}

The $ prefix creates a Binding<Int>. Without it, PageViewController receives an Int and cannot observe future changes.

3. Avoid the “force refresh” hacks

You may see suggestions like adding @State var updater = false and toggling it to force a redraw. Do not do this. It works around the real issue (missing updateUIViewController or incorrect binding) and creates fragile code that breaks with SwiftUI updates. The fixes above are the idiomatic, supported approach.

How to prevent it

  • Always implement updateUIViewController(_:context:) in UIViewControllerRepresentable when the wrapped controller needs to react to SwiftUI state.
  • Use @Binding (or @Bindable in iOS 17+) for any value that must stay in sync between SwiftUI and the representable.
  • Keep view controllers lightweight in makeUIViewController; put all configuration that depends on SwiftUI state in updateUIViewController.
  • Test programmatic state changes (button taps, timers, network callbacks) not just user interactions.

FAQ

Why does my SwiftUI view update but the UIKit controller inside doesn’t?

SwiftUI re-renders the parent view when @State changes, but the UIViewControllerRepresentable only updates its wrapped controller if you implement updateUIViewController and read the latest bound values there.

Can I use @Observable (iOS 17) instead of @Binding for the representable?

@Observable classes work great for SwiftUI views, but UIViewControllerRepresentable still expects @Binding for two-way primitive values. Wrap the observable property in a binding when passing to the representable: Binding(get: { model.page }, set: { model.page = $0 }).

What if updateUIViewController is called too often and causes animation glitches?

Guard the update with a check: only call setViewControllers when the target page differs from the currently visible one. Store the last synced page in the Coordinator to avoid redundant animations.

Sources & credits

Parts of this guide are adapted from content licensed under CC BY-SA 4.0; this article is shared under the same license.