I have a blank Swift project in Xcode and am not using storyboard. I have a view that I've added in code under viewDidLoad:
I then add constraints:
The view loads and looks fine in the simulator. But I've been having some issues with some other views in another project so I have built the above for testing. So what I've discovered is that even though the above builds and works, if I check for ambiguous layouts using:
print(view1.hasAmbiguousLayout)
I get errors logged:
The only way I can get these errors to NOT get logged, is by also setting the ViewController's main view as follows:
view.translatesAutoresizingMaskIntoConstraints = false
By doing this, I know get the following log output:
So my question is this... I haven't read anywhere in Apple's documentation (or here, Googling etc) that says you have to set the viewController's view.translatesAutoresizingMaskIntoConstraints to false. The documentation only talks about views that are added . Have I missed something? Apologies if this is just my understanding but would be good to get this clarified.
Code Block var view1 = UIView() view1.backgroundColor = .red view.addSubview(view1) view1.translatesAutoresizingMaskIntoConstraints = false
I then add constraints:
Code Block NSLayoutConstraint.activate([ view1.topAnchor.constraint(equalTo: view.topAnchor, constant: 20), view1.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 20), view1.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: -20), view1.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: -20) ])
The view loads and looks fine in the simulator. But I've been having some issues with some other views in another project so I have built the above for testing. So what I've discovered is that even though the above builds and works, if I check for ambiguous layouts using:
print(view1.hasAmbiguousLayout)
I get errors logged:
Code Block 2021-02-14 15:26:34.919048+1100 autolayoutextensions[6603:4438601] [LayoutConstraints] View has an ambiguous layout. See "Auto Layout Guide: Ambiguous Layouts" for help debugging. Displaying synopsis from invoking -[UIView _autolayoutTrace] to provide additional detail. *UIView:0x13360c3b0- AMBIGUOUS LAYOUT for UIView:0x13360c3b0.Width{id: 9}, UIView:0x13360c3b0.Height{id: 12} Legend: * - is laid out with auto layout + - is laid out manually, but is represented in the layout engine because translatesAutoresizingMaskIntoConstraints = YES • - layout engine host true
The only way I can get these errors to NOT get logged, is by also setting the ViewController's main view as follows:
view.translatesAutoresizingMaskIntoConstraints = false
By doing this, I know get the following log output:
Code Block false
So my question is this... I haven't read anywhere in Apple's documentation (or here, Googling etc) that says you have to set the viewController's view.translatesAutoresizingMaskIntoConstraints to false. The documentation only talks about views that are added . Have I missed something? Apologies if this is just my understanding but would be good to get this clarified.