3

I have an application which works fine in iPhone and iPad. But when the app is running in split mode I want to update the constraints for the view whenever we change the split mode window size from 1/3,2/3 and so on. How we can know that the app is working on these screens and update constraints accordingly. In the case of split mode changing the view is not updating with the constraints.

I have used below code to recognise the orientation change and update constraints successfully and its working fine.

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

          if UIDevice.current.orientation.isLandscape {
              containerWidthConstraintForLandscape = NSLayoutConstraint(item: containerView, attribute: .width, relatedBy: .equal, toItem: view, attribute: .width, multiplier: 0.5, constant: 0)
              NSLayoutConstraint.activate([containerWidthConstraintForLandscape])
              NSLayoutConstraint.deactivate([containerWidthConstraintForPortrait])
           }
          else {
             containerWidthConstraintForPortrait = NSLayoutConstraint(item: containerView, attribute: .width, relatedBy: .equal, toItem: view, attribute: .width, multiplier: 0.7, constant: 0)
             NSLayoutConstraint.deactivate([containerWidthConstraintForLandscape])
             NSLayoutConstraint.activate([containerWidthConstraintForPortrait])
          }

    }

It would be great if someone could tell me how i can update these constraints while changing the window size in Split Mode/ Slide Over in iPad. Thanks in Advance

Vishnu M Menon
  • 1,459
  • 2
  • 19
  • 34
  • Upvoted, in case if I'm (a) misunderstanding the issue or (b) have a bug I've never seen in my code. I tend to use `viewWillLayoutSubviews()` instead. Yep, it's called repeatedly, but if needed I'll make sure *my* code is only called once. Try this - it works for me. –  Mar 29 '18 at 04:20
  • @dfd This was my scenario. I was having 10 buttons which were distributed evenly in my view. Its work fine in every device and in the case of iPads the size of each buttons were very huge. So in this case I have managed to handle those by giving a constraint to the view that was holding the buttons to occupy the half of the screen in the case of iPads and now the button size is as good as it is in the iPhones. But in the case of Split mode we can always change the windows size from 2/3 to 1/3 by dragging the bounds of the application. In this case the constraints is not working – Vishnu M Menon Mar 29 '18 at 04:27
  • @dfd And I want to catch the screen window changes and update the constraints Anyway I will look into the solution you have given and will update you Thanks – Vishnu M Menon Mar 29 '18 at 04:28

0 Answers0