Обновление ограничений автопоставки при автоповороте - PullRequest
0 голосов
/ 10 октября 2019

У меня есть разные наборы ограничений для автоматического размещения в портретном и ландшафтном режимах. Я активирую и деактивирую их при вращении, но все еще получаю ошибку неудовлетворительных ограничений для деактивированных ограничений. Вот что я делаю:

  //Initialization


    slider.translatesAutoresizingMaskIntoConstraints = false

    view.addSubview(slider)


    let landscapeConstraintTrailing = slider.trailingAnchor.constraint(equalTo: topPanel.trailingAnchor, constant: 0)
    let landscapeConstraintTop = slider.topAnchor.constraint(equalTo: topPanel.bottomAnchor, constant: 5)

    let landscapeConstraintBottom = slider.bottomAnchor.constraint(equalTo: bottomPanel.topAnchor, constant: -5)
    let landscapeConstraintWidth = slider.widthAnchor.constraint(equalToConstant: 140)

     self.sliderLandscapeConstraints = [landscapeConstraintTrailing,
                                          landscapeConstraintTop,
                                          landscapeConstraintBottom,
                                          landscapeConstraintWidth]

     for constraint in self.sliderLandscapeConstraints! {
           constraint.isActive = false
       }

   let portraitConstraintTrailing = slider.trailingAnchor.constraint(equalTo: bottomPanel.trailingAnchor, constant: -10)
   let portraitConstraintBottom = slider.bottomAnchor.constraint(equalTo: bottomPanel.topAnchor, constant: -10)
   let portraitConstraintLeading = slider.leadingAnchor.constraint(equalTo: bottomPanel.leadingAnchor, constant: 10)
   let portraitconstraintHeight = slider.heightAnchor.constraint(equalToConstant: 140)

     self.sliderPortraitConstraints = [portraitConstraintTrailing,
                                         portraitConstraintBottom,
                                         portraitConstraintLeading,
                                         portraitconstraintHeight
                                        ]



     for constraint in self.sliderPortraitConstraints! {
          constraint.isActive = false
     }

А теперь на автозапуске я делаю:

   override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {

        let orientation = UIApplication.shared.statusBarOrientation

          super.viewWillTransition(to: size, with: coordinator)



         coordinator.animate(alongsideTransition: { [unowned self] (_) in

        let orient = UIApplication.shared.statusBarOrientation

        layoutSlider(orient)

    }, completion: { [unowned self] (UIViewControllerTransitionCoordinatorContext) -> Void in

        let orient = UIApplication.shared.statusBarOrientation
        layoutSlider(orient)

    })


private func layoutSlider(_ orient:UIInterfaceOrient) {

    if orient.isLandscape {
         for constraint in self.sliderLandscapeConstraints! {
                constraint.isActive = true
          }

         for constraint in self.sliderPortraitConstraints! {
               constraint.isActive = false
         }
    } else {

         for constraint in self.sliderLandscapeConstraints! {
                constraint.isActive = false
          }

         for constraint in self.sliderPortraitConstraints! {
               constraint.isActive = true
         }


       }

  }

И вот ошибки, которые я получаю при автоповороте:

      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.

2019-10-10 20:12:22.371021+0530 MyApp[1844:173193] [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. 
  (
  "<NSLayoutConstraint:0x2810051d0 UIView:0x103a1d130.leading == UILayoutGuide:0x280a28b60'UIViewSafeAreaLayoutGuide'.leading + 5   (active)>",
  "<NSLayoutConstraint:0x2810014f0 UIView:0x103a1d130.trailing == UILayoutGuide:0x280a28b60'UIViewSafeAreaLayoutGuide'.trailing + 5   (active)>",
  "<NSLayoutConstraint:0x28101e490 MyApp.slider:0x103b2deb0.trailing == UIView:0x103a1d130.trailing - 10   (active)>",
  "<NSLayoutConstraint:0x28101e3a0 MyApp.slider:0x103b2deb0.leading == UIView:0x103a1d130.leading + 10   (active)>",
  "<NSLayoutConstraint:0x28101e350 MyApp.slider:0x103b2deb0.width == 140   (active)>",
  "<NSLayoutConstraint:0x281060690 'UIView-Encapsulated-Layout-Width' UIView:0x103b23410.width == 812   (active)>",
  "<NSLayoutConstraint:0x281001900 'UIViewSafeAreaLayoutGuide-left' H:|-(44)-[UILayoutGuide:0x280a28b60'UIViewSafeAreaLayoutGuide'](LTR)   (active, names: '|':UIView:0x103b23410 )>",
    "<NSLayoutConstraint:0x2810019a0 'UIViewSafeAreaLayoutGuide-right' H:[UILayoutGuide:0x280a28b60'UIViewSafeAreaLayoutGuide']-(44)-|(LTR)   (active, names: '|':UIView:0x103b23410 )>"

)

Will attempt to recover by breaking constraint 
   <NSLayoutConstraint:0x28101e350 MyApp.slider:0x103b2deb0.width == 140   (active)>

1 Ответ

1 голос
/ 10 октября 2019

Порядок важен, вы должны деактивировать, а затем активировать, чтобы не создавать конфликты

private func layoutSlider(_ orient:UIInterfaceOrient) {

    if orient.isLandscape {
         // false should be first
         for constraint in self.sliderPortraitConstraints! {
               constraint.isActive = false
         }

         for constraint in self.sliderLandscapeConstraints! {
                constraint.isActive = true
          }

    } else {
          // false should be first
         for constraint in self.sliderLandscapeConstraints! {
                constraint.isActive = false
          }

         for constraint in self.sliderPortraitConstraints! {
               constraint.isActive = true
         } 

       } 
}
...