I’m currently trying to use Storyboards with KMM. ...
# kotlin-native
l
I’m currently trying to use Storyboards with KMM. I’m aware that I can’t attach my Kotlin ViewController to the storyboard, so I’m trying to load the storyboard using
UIStoryboard.storyboardWithName(storyboardName, null).instantiateInitialViewController()
and calling view.addSubview on the main view of the resulting ViewController in my Kotlin ViewController. This works fine, and I’ve set up nice extension functions for this, but I’m seeing that the Safe Area is ignored. I set up rotation lately, reloading the storyboard on device rotation, and I noticed that the Safe Area works this time. Has anyone tried something similar and run into the same issues?
My goal is to avoid Swift entirely in this project, using Kotlin to define the ViewControllers instead.
s
So you’re loading a plain UIViewController and then using view controller containment to add your Kotlin view controller? When you add your Kotlin vc’s view to the parent, you should constrain it not to the parent view’s leading, trailing, top & bottom anchors but to it’s safe area insets instead. However this is going to leave you with some white space at the bottom of the phone that your view controller can’t fill. Really what you want is to constrain your kotlin vc view fully inside the parent and then constrain your kotlin vc’s child views to the safe area insets. Make sure you’re following the proper steps for vc containment. 1. Instantiate VC 2. Call
addChild(_ childController: UIViewController)
on the parent with your Kotlin vc as the child. 3. Call
child.view.translatesAutoresizingMasksIntoContraints = false
4. Call
parent.view.addSubView(child.view)
5. Constrain your view in the parent as you need 6. Important: Call
child.didMove(toParent: parent)
Child is your Kotlin VC and parent is the plain UIViewController in these steps.
l
I’ll have to look into doing this. I hadn’t thought of keeping the instantiated VC as a parent. Before, I had just added the root view of the instantiated controller as a subview of my Kotlin VC.
s
Either way should work as long as you are accounting for the safe area insets in your constraints and following the steps for vc containment. If you don’t follow the last step, the vc doesn’t receive important system notifications and things like
viewDidAppear
won’t fire.
l
That makes sense.
s
Xcode’s view debugging works well for diagnosing constraint issues. There’s a third party tool call Reveal that is even better. I’m not affiliated with them, I just like the product.
l
Adding the instantiated VC as a child of my Kotlin VC and calling didMove on the instantiated one fixed my issue. It looks like the instantiated VC didn’t have the info it needed to layout its safe area. It’s odd that after rotation, the safe area worked.
There’s still some constraint warnings. One of my coworkers who works with iOS is looking at fixing those (I’m primarily an Android dev, so constraints are new to me). Your suggestion did fix the visual issue I was getting, though.
s
Autolayout can be quite a pain sometimes. The main thing to remember is that everything needs to be able to calculate the x, y, height, and width of a view in a way that isn’t ambiguous.
130 Views