0

I just start to study swift, and when I use Cartography to autolayout views, I have a problem. Here is what happens.

I create a LoginViewController, put all the constrains in a function named "updateConstrains()", and called in the override func viewDidLoad(). If I set the LoginViewController as the window rootViewController in AppDelegate, it works well.

The right layout

But if I put the LoginViewController as the rootViewController of a UINavigationController as follows:

window!.rootViewController = UINavigationController(rootViewController: loginViewController)

It become like this:

The autolayout has no effect

and when print the width and height of the LoginViewController's view. It turns out that:

width = 155, height = 0.

Can anyone tell me what happens?

pableiros
  • 14,932
  • 12
  • 99
  • 105
luosk
  • 13
  • 3

1 Answers1

0

A common cause for this is forgetting to set translatesAutoresizingMaskIntoConstraints = false on the view.

Views which are instanced in code have this property set to true by default. This creates a set of auto layout constraints based on the values of autoresizingMask. If the autoresizing mask is not correctly defined, which is often the case when auto-layout is used, then the generated constraints will not be what you intended.

See the Apple documentation for translatesAutoresizingMaskIntoConstraints and autoresizingMask

Luke Van In
  • 5,215
  • 2
  • 24
  • 45