In UIKit, how does UIHostingConfiguration put a SwiftUI row inside a UICollectionView cell, and how should the row update when its data changes?
answer
- a content configuration, not a controller
- contentConfiguration on the cell
- cell state goes in the handler
- reconfigure, do not reload
- iOS 16 floor
basics
~20 sUIHostingConfiguration, iOS 16, is a UIContentConfiguration whose content is a SwiftUI view; you assign it to a cell's contentConfiguration. When data changes you reconfigure the item so a fresh configuration is built, or let an observed model update the row.
solid answer
~40 s`UIHostingConfiguration` (iOS 16) conforms to `UIContentConfiguration`, so you assign `cell.contentConfiguration = UIHostingConfiguration { ProductRow(product: product) }` in a cell registration, for collection or table view cells alike, with no child view controller per cell. Modifiers on the configuration set `margins(_:_:)`, `background(content:)` and `minSize(width:height:)`, and in a list layout swipe actions and separator alignment are bridged. The row should be a function of its item and the cell state: build it in `configurationUpdateHandler` to read `state.isSelected`. When an item's data changes, apply a diffable snapshot with `reconfigureItems(_:)`, which re-runs the registration for the existing cell, or have the row read an observable model so SwiftUI updates it in place.
code
swift · 34 linesimport SwiftUI
import UIKit
struct Product: Hashable {
let id: UUID
var name: String
var price: Decimal
}
struct ProductRow: View {
let product: Product
let isSelected: Bool
var body: some View {
HStack {
Text(product.name)
Spacer()
Text(product.price, format: .currency(code: "EUR"))
}
.fontWeight(isSelected ? .semibold : .regular)
}
}
@MainActor
func makeProductCellRegistration() -> UICollectionView.CellRegistration<UICollectionViewListCell, Product> {
UICollectionView.CellRegistration { cell, _, product in
cell.configurationUpdateHandler = { cell, state in
cell.contentConfiguration = UIHostingConfiguration {
ProductRow(product: product, isSelected: state.isSelected)
}
.margins(.horizontal, 16)
}
}
}go deeper
Recall that the SwiftUI row goes into the cell through contentConfiguration, built with a UIHostingConfiguration closure.
Explain how the configuration is rebuilt: the registration handler, configurationUpdateHandler for cell state, and reconfigureItems for data changes.
Diagnose rows that show stale data or wrong heights by checking how updates reach the cell and whether self-sizing invalidation is allowed.
Weigh SwiftUI rows inside an existing UIKit list as a low-risk first migration step against the deployment-target floor it imposes.
## What UIHostingConfiguration is Since iOS 14, UIKit cells can describe their content with **content configurations**: values that conform to `UIContentConfiguration` and that you assign to a cell's `contentConfiguration` property. The cell asks the configuration for a content view and updates it when a new configuration arrives. **`UIHostingConfiguration`**, added in **iOS 16**, is a content configuration whose content is a **SwiftUI view hierarchy**. Apple's documentation says to use it with a `UICollectionViewCell` or `UITableViewCell` to host SwiftUI views in a collection or table view. It is a value type, generic over the content view and a background view, so there is **no view controller per cell** and no child containment to manage across reuse, which is what made earlier approaches with a hosting controller inside each cell fragile. ## Building a row The usual place is a **cell registration**: 1. Create `UICollectionView.CellRegistration<UICollectionViewListCell, Product>`. 2. In its handler, assign `cell.contentConfiguration = UIHostingConfiguration { ProductRow(product: product) }`. 3. Dequeue with `dequeueConfiguredReusableCell(using:for:item:)` from the diffable data source's cell provider. The configuration has its own modifiers, separate from SwiftUI view modifiers on the content: | Modifier | Effect | |---|---| | `margins(_:_:)` | Insets between the cell's edges and the SwiftUI content, by edges and a length or `EdgeInsets` | | `background(content:)` / `background(_:)` | A SwiftUI view or shape style drawn as the cell's background | | `minSize(width:height:)` | The smallest size the content may take; the parameterless `minSize()` is deprecated in favour of passing values | When the cell is used in a **list layout**, the documentation notes that some SwiftUI APIs are bridged automatically, such as **swipe actions** declared with `swipeActions` on the content and **separator alignment**. ## Reacting to cell state Selection, highlighting and editing are **cell state**, owned by UIKit. To render them in SwiftUI, build the configuration inside the cell's `configurationUpdateHandler` (iOS 15), which receives a `UICellConfigurationState`: - read `state.isSelected` or `state.isHighlighted`; - pass it into the row as a plain parameter, such as `ProductRow(product: product, isSelected: state.isSelected)`. The row then stays a **pure function of its inputs**: the item and the cell state. `UIHostingConfiguration` also implements `updated(for:)`, the `UIContentConfiguration` hook a cell calls with its new state. ## When the data changes There are two sound ways for a row to show new data: 1. **Reconfigure the item.** Apply a diffable snapshot with `reconfigureItems(_:)` (iOS 15). It re-runs the registration handler for the existing cell, so a new hosting configuration with the new product is assigned. This is lighter than `reloadItems(_:)`, which replaces the cell. 2. **Let SwiftUI observe a model.** If the row reads a reference-type model, an `@Observable` class from iOS 17 or an `ObservableObject`, SwiftUI updates the row in place when a property it reads changes, with no snapshot at all. What to avoid: - `reloadItems(_:)` or a full `reloadData()` for every small change; it discards the existing cell. - Deleting and re-inserting an identifier to "refresh" it, which animates a removal and an insertion instead of an update. - Hiding item data in `@State` inside the row, where it is no longer derived from the item the data source holds. ## Sizing in a list List-layout cells **self-size**: the collection view asks the cell for its size, and the hosting configuration's content view answers from the SwiftUI content. If the content changes size **without** a reconfigure, for example when an observed model makes a label wrap onto two lines, whether the cell may resize in place depends on the collection view's **`selfSizingInvalidation`** (iOS 16). With `.disabled`, invalidating a self-sizing cell's size triggers no update; with `.enabled` or `.enabledIncludingConstraints`, the cell is resized when needed. ## The version floor Because both `UIHostingConfiguration` and `selfSizingInvalidation` are **iOS 16**, an app that still supports iOS 15 needs a fallback, for example a `UIListContentConfiguration` or a custom UIKit cell inside an `if #available(iOS 16, *)` branch. On iOS 16 and later it is the standard way to bring SwiftUI into an existing UIKit list without rewriting the screen, which makes it a common first step when a team starts adopting SwiftUI. ## What interviewers listen for - That it is a **content configuration**, assigned to `contentConfiguration`, not a controller. - Cell state flowing in through `configurationUpdateHandler`. - `reconfigureItems(_:)` rather than reloading for data changes. - Awareness of the iOS 16 floor.
- Why not embed a UIHostingController in each collection view cell instead?A hosting controller in a cell needs child containment managed across reuse, which is fiddly and easy to get wrong. `UIHostingConfiguration` was added in iOS 16 for this case: it is a value you assign to `contentConfiguration`, needs no view controller and works with the cell's reuse and configuration-state machinery.
- A row's SwiftUI content grows when an observed model changes, but the cell keeps its old height; what do you check?The collection view's `selfSizingInvalidation` (iOS 16). When it is `.disabled`, a self-sizing cell is not resized in place when its content invalidates its size, so the new height waits for the next layout pass. With it enabled, or after `reconfigureItems(_:)`, the cell is measured again.
A hosting configuration is like a recipe card clipped to a reusable tray: the tray (the cell) stays, and each time the order changes you clip on a new card rather than building a new tray.
- Calling reloadItems for a data change when reconfigureItems would keep the existing cell.
- Adding a UIHostingController's view to every cell's contentView without containment.
- Believing UIHostingConfiguration needs a view controller to own it.
- Reading selection state inside the SwiftUI row instead of from UICellConfigurationState.
- Shipping UIHostingConfiguration in an app whose deployment target is below iOS 16 without a fallback.
Assumes the iOS 27 SDK; UIHostingConfiguration arrived in iOS 16, so an app that still supports iOS 15 needs a UIKit fallback behind if #available.