I see some weird behavior concerning translatesAutoresizingMaskIntoConstraints when using addChild.
I created a UITableViewController programmatically, put it inside a UINavigationController, also created programmatically, so no storyboards or anything like that involved. Then, when I added the navigation controller as a child, everything worked perfectly. See here the code I used:
//Add Child View Controller
self.addChild(viewController)
//Add Child View as Subview
self.view.addSubview(viewController.view)
print("TranslatesAutoresizingMaskIntoConstraints: \(viewController.view.translatesAutoresizingMaskIntoConstraints)")
//Define Constraints
NSLayoutConstraint.activate([
viewController.view.topAnchor.constraint(equalTo: self.view.topAnchor),
viewController.view.bottomAnchor.constraint(equalTo: self.view.bottomAnchor),
viewController.view.leadingAnchor.constraint(equalTo: self.view.leadingAnchor),
viewController.view.trailingAnchor.constraint(equalTo: self.view.trailingAnchor)
])
//Notify Child View Controller
viewController.didMove(toParent: self)
Then I discovered I actually didn't need the navigationController, so I just added my tableviewcontroller itself as a child. Suddenly, it didn't work anymore, and I saw an empty screen. I printed out the frame, which was 0,0,0,0. Then, I remembered that when I use 'NSLayoutConstraint.activate', I need to set 'translatesAutoresizingMaskIntoConstraints' to false.
So I did, and, like I expected, it worked again. But it felt strange to me, because as far as I knew, UINavigationControllers didn't automatically set translatesAutoresizingMaskIntoConstraints to false. So I put it back into the navigation controller and printed out the value for translatesAutoresizingMaskIntoConstraints, and to my surprise, the value showed said it was set to true! But somehow it still works and I see the tableviewcontroller perfectly!
I also printed out the value of translatesAutoresizingMaskIntoConstraints in viewDidAppear and other places, but everywhere the printed value is true. Does UINavigationController magically do some stuff somewhere?
translatesAutoresizingMaskIntoConstraintsto be false when you add AutloLayout constraints. It is true by default, and the storyboard sets it to false when you add constraints on the storyboard. If there are no constraints, andtranslatesAutoresizingMaskIntoConstraintsshould be true.