0

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?

10
  • You should show a minimal reproducible example for 3 and 4. Did you also add constraints to the view in the storyboard? If not, I don't see why this is unexpected. Commented Aug 29, 2023 at 12:16
  • @Sweeper thanks for your reply. I can create a minimal example, but I was hoping I was just missing something. As for the storyboard, no I didn't make any constraints. But why do you say it's as you expect? I thought the storyboard one would automatically set it to false, but apparently, as I print it out, it's not the case. But it still works somehow... Commented Aug 29, 2023 at 12:20
  • You only need translatesAutoresizingMaskIntoConstraints to 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, and translatesAutoresizingMaskIntoConstraints should be true. Commented Aug 29, 2023 at 12:23
  • But I DO add constraints as you can see in my code. Commented Aug 29, 2023 at 12:25
  • I thought we are talking about the storyboard? Are you using both at the same time? This is why a minimal reproducible example is important. Commented Aug 29, 2023 at 12:27

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.