Chuck Pinkert
Chuck Pinkert

Reputation: 1345

Why does my auto-layout ViewController display incorrectly when placed in a NavigationController?

I have an extremely simple application... I'm learning auto-layout so I created a viewController that has a white background, a button, and a textfield. I added constraints to the button and textfield to put them on the top of the parent view (the viewcontrollers view) and right next to each other. Worked perfectly...

Then I tried to push this viewController into a navigationController and everything went to crap. The background color is now black (instead of white) and the button and textfield no longer acknowledged their auto-layout constraint to the top the parent view. Why is this? How can I get the viewController to behave like it does outside of the nav controller?

If I do the same sort of layout by slinging CRects around everything works perfectly as I would expect it to.

EDIT: Found another fun artifact, in auto-layout mode, if I rotate the simulator the layout fixes itself and its perfect, if I rotate it back to portrait it stays exactly as I thought it should be (white and aligned to the top). Maybe I should just load the view, and rotate it sideways and back right, ha!

psudeocode:

--appdelegate--    

window.Root = new uinavcontroller with AutoLayoutController as root
window.makewindowkeyandvisible


--autolayoutcontroller--    

In ViewDidLoad
view.backgroundcolor = white
add(new button)
add(new textfield)
view.addConstraints(new constraint (button.top == view.top + padding)
view.addConstraints(new constraint (textfield.top == view.top + padding)
view.addConstraints(new constraint (textfield.right = button.left)

Structs and Springs showing what I'd expect: Stucts and springs

AutoLayout crapping the bed: AutoLayout fail

Upvotes: 1

Views: 493

Answers (1)

Chuck Pinkert
Chuck Pinkert

Reputation: 1345

I was setting the ViewController's view to not TranslatesAutoresizingMaskIntoConstraints. This made the view turn into an auto layout view, which gave it a height and width of 0,0. After turning that off and going back to autosizing for just the base view of the viewcontroller, and using auto-layout for all the children... it worked as expected.

Upvotes: 1

Related Questions