Tips: Add SwiftUI View into Xib View and use SwiftUI content size

Since Apple introduce SwiftUI in WWDC 2019, I really exciting about the new way to build the UI. I started to learn and did some demo from that time. But the problem is SwiftUI requires iOS 13 and above, therefore at that time, I could not apply it to our existing app which still supports iOS 11.

Nowadays, when the number of users of iOS 12 is really low, our client agreed to update the minimum support version to iOS 13. So now, we are applying SwiftUI to the existing UIKit source code.

This is a small tip we are using to add SwiftUI View to a xib file. First, we need a base class to convert from SwiftUI to UIView

And when we finished the SwiftUI View, we will wrap it into UIView

Then, we start adding custom view into Xib view and set a height constraint for it (to remove the constraint error)

After that, in ViewController, we call

communityInfoHeight.isActive = false

to remove the height constraint. Now the view will resize follow the SwiftUI View.

This is our tip to add the SwiftUI View into Xib file and use the content size of SwiftUI. Thanks for reading and If you have any better solution, please recommend it for me.

Music, Reading and Writing Code