I found SwiftUI Text
views to be extremely easy to create Labels with custom designs. So I wanted to use it as a view to a regular UIKit UICollectionViewCell.
This is my code so far (you can copy and paste inside Xcode 11).
import SwiftUI
import UIKit
struct ContentView: View {
var body: some View {
CollectionComponent()
}
}
#if DEBUG
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
#endif
struct CollectionComponent : UIViewRepresentable {
func makeCoordinator() -> CollectionComponent.Coordinator {
Coordinator(data: [])
}
class Coordinator: NSObject, UICollectionViewDataSource, UICollectionViewDelegate {
var data: [String] = []
init(data: [String]) {
for index in (0...1000) {
self.data.append("\(index)")
}
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
data.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! GenericCell
cell.customView.rootView = AnyView(
Text(data[indexPath.item]).font(Font.title).border(Color.red)
)
return cell
}
}
func makeUIView(context: Context) -> UICollectionView {
let layout = UICollectionViewFlowLayout()
layout.estimatedItemSize = UICollectionViewFlowLayout.automaticSize
layout.scrollDirection = .vertical
let cv = UICollectionView(frame: .zero, collectionViewLayout: layout)
cv.dataSource = context.coordinator
cv.delegate = context.coordinator
cv.register(GenericCell.self, forCellWithReuseIdentifier: "cell")
cv.backgroundColor = .white
layout.minimumInteritemSpacing = 0
layout.minimumLineSpacing = 0
return cv
}
func updateUIView(_ uiView: UICollectionView, context: Context) {
}
}
open class GenericCell: UICollectionViewCell {
public var customView = UIHostingController(rootView: AnyView(Text("")))
public override init(frame: CGRect) {
super.init(frame: frame)
configure()
}
public required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
configure()
}
private func configure() {
contentView.addSubview(customView.view)
customView.view.preservesSuperviewLayoutMargins = false
customView.view.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
customView.view.leftAnchor.constraint(equalTo: contentView.layoutMarginsGuide.leftAnchor),
customView.view.rightAnchor.constraint(equalTo: contentView.layoutMarginsGuide.rightAnchor),
customView.view.topAnchor.constraint(equalTo: contentView.layoutMarginsGuide.topAnchor),
customView.view.bottomAnchor.constraint(equalTo: contentView.layoutMarginsGuide.bottomAnchor),
])
}
}
The first screen is good.
But as i scroll past the end of visible screen it looks like
Is there something i am doing wrong with autoresizing my cells? Or is this just more SwiftUI bugs?
[Edit] I have accepted a SwiftUI answer, but If any one can provide me a fix to work with UIKit as asked in this question, I will accept.