# SwiftUIHostingConfiguration

In iOS 16, Apple added [UIHostingConfiguration](https://developer.apple.com/documentation/SwiftUI/UIHostingConfiguration), making it straightforward to use a SwiftUI view in instances of UICollectionViewCell and UITableViewCell. This gist shows how UIHostingConfiguration can be backported to iOS 14 by implementing a type that conforms to [UIContentConfiguration](https://developer.apple.com/documentation/uikit/uicontentconfiguration).

Starting from iOS 16, we can use SwiftUI views in an instance of UICollectionView or UITableViewCell like this:

```swift
cell.contentConfiguration = UIHostingConfiguration {
    ExampleCellContentView(color: Color(item.color), text: item.text)
}
```

With the SwiftUIHostingConfiguration, we can use a similar API starting from iOS 14.

```swift
cell.contentConfiguration = SwiftUIHostingConfiguration(parentViewController: self) {
    ExampleCellContentView(color: Color(item.color), text: item.text)
}
```

We pass an instance of our view controller because, under the hood, the SwiftUIHostingConfiguration creates a UIHostingController and inserts it into the view controller hierarchy.

![Screen recording of the iPhone simulator shown a collection view with cells configured using SwiftUIHostingConfiguration.](https://gist.github.com/user-attachments/assets/82891898-1153-4d93-82a2-d52385193538)