I might have done that like hundreds of time but I have started realizing that, with a brand new project on Xcode 11, when I try to set up my constraints on a UIViewController I get constraints issues.
My code:
class ViewController: UIViewController {
private lazy var button: UIButton = {
let button = UIButton()
button.setTitle("Click me!", for: .normal)
return button
}()
init() {
super.init(nibName: nil, bundle: nil)
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = .red
view.addSubview(button)
button.centerYAnchor.constraint(equalTo: view.centerYAnchor).isActive = true
button.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
button.heightAnchor.constraint(equalToConstant: 52).isActive = true
button.widthAnchor.constraint(equalToConstant: 128).isActive = true
}
}
And it is called from my SceneDelegate
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
if let windowScene = scene as? UIWindowScene {
let window = UIWindow(windowScene: windowScene)
let timeline = ViewController()
let navigation = UINavigationController(rootViewController: timeline)
window.rootViewController = navigation
self.window = window
window.makeKeyAndVisible()
}
}
The console message:
2020-02-25 10:18:00.250858+0000 TestScene[374:17466] [LayoutConstraints] Unable to simultaneously satisfy constraints.
Probably at least one of the constraints in the following list is one you don't want.
Try this:
(1) look at each constraint and try to figure out which you don't expect;
(2) find the code that added the unwanted constraint or constraints and fix it.
(Note: If you're seeing NSAutoresizingMaskLayoutConstraints that you don't understand, refer to the documentation for the UIView property translatesAutoresizingMaskIntoConstraints)
(
"<NSAutoresizingMaskLayoutConstraint:0x283144be0 h=--& v=--& UIButton:0x119f199d0'Click me!'.minX == 0 (active, names: '|':UIView:0x119f19860 )>",
"<NSAutoresizingMaskLayoutConstraint:0x283144c30 h=--& v=--& UIButton:0x119f199d0'Click me!'.width == 0 (active)>",
"<NSLayoutConstraint:0x283144640 UIButton:0x119f199d0'Click me!'.centerX == UIView:0x119f19860.centerX (active)>",
"<NSLayoutConstraint:0x283145310 'UIView-Encapsulated-Layout-Width' UIView:0x119f19860.width == 414 (active)>"
)
Will attempt to recover by breaking constraint
<NSLayoutConstraint:0x283144640 UIButton:0x119f199d0'Click me!'.centerX == UIView:0x119f19860.centerX (active)>
Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger.
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKitCore/UIView.h> may also be helpful.
2020-02-25 10:18:00.251321+0000 TestScene[374:17466] [LayoutConstraints] Unable to simultaneously satisfy constraints.
Probably at least one of the constraints in the following list is one you don't want.
Try this:
(1) look at each constraint and try to figure out which you don't expect;
(2) find the code that added the unwanted constraint or constraints and fix it.
(Note: If you're seeing NSAutoresizingMaskLayoutConstraints that you don't understand, refer to the documentation for the UIView property translatesAutoresizingMaskIntoConstraints)
(
"<NSAutoresizingMaskLayoutConstraint:0x283145270 h=--& v=--& UIButton:0x119f199d0'Click me!'.minY == 0 (active, names: '|':UIView:0x119f19860 )>",
"<NSAutoresizingMaskLayoutConstraint:0x2831452c0 h=--& v=--& UIButton:0x119f199d0'Click me!'.height == 0 (active)>",
"<NSLayoutConstraint:0x283144140 UIButton:0x119f199d0'Click me!'.centerY == UIView:0x119f19860.centerY (active)>",
"<NSLayoutConstraint:0x283145360 'UIView-Encapsulated-Layout-Height' UIView:0x119f19860.height == 896 (active)>"
)
Will attempt to recover by breaking constraint
<NSLayoutConstraint:0x283144140 UIButton:0x119f199d0'Click me!'.centerY == UIView:0x119f19860.centerY (active)>
Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger.
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKitCore/UIView.h> may also be helpful.
2020-02-25 10:26:19.319362+0000 TestScene[374:17466] Can't end BackgroundTask: no background task exists with identifier 1 (0x1), or it may have already been ended. Break in UIApplicationEndBackgroundTaskError() to debug.
What am I doing wrong?
private lazy var button: UIButton = {
let button = UIButton()
button.setTitle("Click me!", for: .normal)
button.translatesAutoResizingMaskIntoConstraints = false
return button
}()