#swift
Вопрос:
Я добавил 4 компонента программно, как показано ниже;
UITextField(.голубой), UIImageView(.зеленый), UILabel(.желтый) и UIView(.синий). Вот метод viewDidLoad;
override func viewDidLoad() { super.viewDidLoad() // 1 view.addSubview(searchTextField) if (isDevelopmentProcess){searchTextField.backgroundColor = .cyan} else {searchTextField.backgroundColor = .clear} searchTextField.translatesAutoresizingMaskIntoConstraints = false searchTextField.leftAnchor.constraint(equalTo: view.leftAnchor, constant: 15).isActive = true searchTextField.rightAnchor.constraint(equalTo: view.rightAnchor, constant: -15).isActive = true searchTextField.heightAnchor.constraint(equalToConstant: 34).isActive = true searchTextField.topAnchor.constraint(equalTo: view.topAnchor, constant: 40).isActive = true // 2 view.addSubview(randomRecordPicture) if (isDevelopmentProcess){randomRecordPicture.backgroundColor = .green} else {randomRecordPicture.backgroundColor = .clear} randomRecordPicture.translatesAutoresizingMaskIntoConstraints = false randomRecordPicture.leftAnchor.constraint(equalTo: view.leftAnchor, constant: 100).isActive = true randomRecordPicture.rightAnchor.constraint(equalTo: view.rightAnchor, constant: -100).isActive = true randomRecordPicture.heightAnchor.constraint(equalToConstant: 140).isActive = true randomRecordPicture.topAnchor.constraint(equalTo: view.topAnchor, constant: 89).isActive = true //3 view.addSubview(randomRecordTitle) if (isDevelopmentProcess){randomRecordTitle.backgroundColor = .yellow} else {randomRecordTitle.backgroundColor = .clear} randomRecordTitle.translatesAutoresizingMaskIntoConstraints = false randomRecordTitle.leftAnchor.constraint(equalTo: view.leftAnchor, constant: 15).isActive = true randomRecordTitle.rightAnchor.constraint(equalTo: view.rightAnchor, constant: -15).isActive = true randomRecordTitle.heightAnchor.constraint(equalToConstant: 34).isActive = true randomRecordTitle.topAnchor.constraint(equalTo: view.topAnchor, constant: 244).isActive = true //4 view.addSubview(blueView) if (isDevelopmentProcess){blueView.backgroundColor = .blue} else {randomRecordTitle.backgroundColor = .clear} blueView.translatesAutoresizingMaskIntoConstraints = false blueView.leftAnchor.constraint(equalTo: view.leftAnchor).isActive = true blueView.rightAnchor.constraint(equalTo: view.rightAnchor).isActive = true blueView.bottomAnchor.constraint(equalTo: view.bottomAnchor).isActive = true blueView.topAnchor.constraint(equalTo: view.topAnchor, constant: 293).isActive = true }
It looks like below after I click rotate button on simulator;
1st time rotate(It is fine.)
2nd time rotate(It is fine.)
3rd time rotate(It is fine.)
4th time rotate(this is the problem);
I would like to see the page like we start after I click rotate button 4th time;
Here is my code block for the rotate issue;
override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) { super.viewWillTransition(to: size, with: coordinator) coordinator.animate(alongsideTransition: nil) { [self] _ in if UIDevice.current.orientation.isLandscape { //LANDSCAPE randomRecordPicture.isHidden = true randomRecordTitle.isHidden = true blueView.topAnchor.constraint(equalTo: view.topAnchor, constant: 89).isActive = true } else{ // PORTRAIT randomRecordPicture.isHidden = false randomRecordTitle.isHidden = false randomRecordPicture.translatesAutoresizingMaskIntoConstraints = false randomRecordPicture.leftAnchor.constraint(equalTo: view.leftAnchor, constant: 100).isActive = true randomRecordPicture.rightAnchor.constraint(equalTo: view.rightAnchor, constant: -100).isActive = true randomRecordPicture.heightAnchor.constraint(equalToConstant: 140).isActive = true randomRecordPicture.topAnchor.constraint(equalTo: view.topAnchor, constant: 89).isActive = true randomRecordTitle.translatesAutoresizingMaskIntoConstraints = false randomRecordTitle.leftAnchor.constraint(equalTo: view.leftAnchor, constant: 15).isActive = true randomRecordTitle.rightAnchor.constraint(equalTo: view.rightAnchor, constant: -15).isActive = true randomRecordTitle.heightAnchor.constraint(equalToConstant: 34).isActive = true randomRecordTitle.topAnchor.constraint(equalTo: view.topAnchor, constant: 244).isActive = true blueView.translatesAutoresizingMaskIntoConstraints = false blueView.leftAnchor.constraint(equalTo: view.leftAnchor).isActive = true blueView.rightAnchor.constraint(equalTo: view.rightAnchor).isActive = true blueView.bottomAnchor.constraint(equalTo: view.bottomAnchor).isActive = true blueView.topAnchor.constraint(equalTo: view.topAnchor, constant: 293).isActive = true randomRecordPicture.layer.zPosition = 3 randomRecordTitle.layer.zPosition = 2 blueView.layer.zPosition = 1 } } }
How can I fix this problem? All views should be in starting position after end of the rotate loop. Looks like below code does not work. Also I see lots of warnings after I click the rotate button.
blueView.topAnchor.constraint(equalTo: view.topAnchor, constant: 293).isActive = true
Here is the warning;
[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. ( "lt;NSLayoutConstraint:0x600003d83520 V:|-(89)-[UIView:0x7fb90970cb20] (active, names: '|':UIView:0x7fb90a304780 )gt;", "lt;NSLayoutConstraint:0x600003d83840 V:|-(293)-[UIView:0x7fb90970cb20] (active, names: '|':UIView:0x7fb90a304780 )gt;" ) Will attempt to recover by breaking constraint lt;NSLayoutConstraint:0x600003d83840 V:|-(293)-[UIView:0x7fb90970cb20] (active, names: '|':UIView:0x7fb90a304780 )gt; Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger. The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in lt;UIKitCore/UIView.hgt; may also be helpful.
By the way it looks like below when i write -293;
blueView.topAnchor.constraint(equalTo: view.topAnchor, constant: -293).isActive = true
But blue view is not going below if I write more then 293 value. Like this
blueView.topAnchor.constraint(equalTo: view.topAnchor, constant: 450).isActive = true
Thanks.