Присвоение ограничений программно завершается неудачей UIViewController Xcode 11 - PullRequest
0 голосов
/ 25 февраля 2020

Я мог бы сделать это как сотни раз, но я начал понимать, что с новым проектом на Xcode 11, когда я пытаюсь установить свои ограничения на UIViewController, у меня возникают проблемы с ограничениями.

Мой код:

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

    }
}

И он вызывается из моего 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()
    }
}

Консольное сообщение:

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.

Что я делаю не так?

1 Ответ

1 голос
/ 25 февраля 2020
private lazy var button: UIButton = {
    let button = UIButton()
    button.setTitle("Click me!", for: .normal)
    button.translatesAutoResizingMaskIntoConstraints = false
    return button
}()
...