Constraints for a child view controller within a subview
Published on · Updated on
1 minute read
When a child UIViewController
is added within one of it's parent's subviews, there can be an issue where it doesn't expand to fill it. This can often happen when we only tested on a specific device size.
The fix involves preventing the auto-resizing mask from being turned into constraints and ensuring the child view controller's anchor constraints are all attached to the edge's of its container view.
addChild(child)
containerView.addSubview(child.view)
child.view.translatesAutoresizingMaskIntoConstraints = false
child.view.attachAnchors(to: containerView)
child.didMove(toParent: self)
I hope the article was useful. If you have any feedback or questions please feel free to reach out to me on Twitter.
Thanks for reading and happy coding!
Hi, I hope you enjoyed the article. I am Andrew - a builder of apps and developer tools. Articles on the blog focus on all aspects of Android and iOS development using Kotlin and Swift.