Various portrait / landscape views in storyboard and fast

I use storyboard and Xcode 6 to create application views, but I ran into this problem: I want to assign different subview positions for portrait and landscape modes. For instance:

PortraitLandscape

Since then I have achieved this programmatically using willRotateToInterfaceOrientation and the status bar to get the ipad orientation.

With Xcode 6, iPhone layouts for portrait and landscape are different, but the same for iPad (regular, regular). Can these positions be reached with restrictions?

Thanks.

+6
source share
3 answers

Yes, you can do it with restrictions.

First you need to create restrictions for the supervisor, not for the closest view. Neighboring views will change, so we DO NOT want the restrictions to apply to other views. See the screenshot below for an example of how to set limits.

Constraint setup

Then bind the constraints that you will change in IBOutlets so that we can modify them programmatically. For your example, these would be limitations:

@IBOutlet var greenViewTrailingConstraint: NSLayoutConstraint! @IBOutlet var greenViewBottomConstraint: NSLayoutConstraint! @IBOutlet var redViewTopConstraint: NSLayoutConstraint! @IBOutlet var redViewLeadingConstraint: NSLayoutConstraint! @IBOutlet var redViewBottomConstraint: NSLayoutConstraint! @IBOutlet var blueViewTrailingConstraint: NSLayoutConstraint! @IBOutlet var blueViewTopConstraint: NSLayoutConstraint! @IBOutlet var blueViewLeadingConstraint: NSLayoutConstraint! 

Finally, update the constraint constants based on UIInterfaceOrientation. Again, using your example, the code looks something like this:

 override func willRotateToInterfaceOrientation(toInterfaceOrientation: UIInterfaceOrientation, duration: NSTimeInterval) { let padding: CGFloat = 16.0 // since we're calling this before the rotation, the height and width are swapped let viewHeight = self.view.frame.size.width let viewWidth = self.view.frame.size.height // if landscape if UIInterfaceOrientationIsLandscape(toInterfaceOrientation) { greenViewTrailingConstraint.constant = (viewWidth/2.0) + (padding/2.0) greenViewBottomConstraint.constant = padding blueViewTopConstraint.constant = (viewHeight/2.0) + (padding/2.0) blueViewTrailingConstraint.constant = padding blueViewLeadingConstraint.constant = (viewWidth/2.0) + (padding/2.0) redViewTopConstraint.constant = padding redViewBottomConstraint.constant = (viewHeight/2.0) + (padding/2.0) redViewLeadingConstraint.constant = (viewWidth/2.0) + (padding/2.0) } else { // else portrait greenViewBottomConstraint.constant = (viewHeight/2.0) + (padding/2.0) greenViewTrailingConstraint.constant = padding blueViewTopConstraint.constant = (viewHeight/2.0) + (padding/2.0) blueViewTrailingConstraint.constant = (viewWidth/2.0) + (padding/2.0) blueViewLeadingConstraint.constant = padding redViewLeadingConstraint.constant = (viewWidth/2.0) + (padding/2.0) redViewBottomConstraint.constant = padding redViewTopConstraint.constant = (viewHeight/2.0) + (padding/2.0) } } 
+7
source

I know this is an old post, but only for info.I have taken the following steps and it works as expected.

Step 1. Select View Controller, select any size control. And add views in this mode. Any mode Step 2. Change the mode to record all iPhone using size class control, add restrictions for the view mode in portrait mode. Potream mode Step 3. Change the mode in the same way to change the landscape of all iPhones, add view restrictions for landscape mode.

Please note: restrictions are now independent for portrait and landscape

+1
source

Click the plus β€œ+” next to the restriction constant and add options for different screens:

enter image description here

0
source

Source: https://habr.com/ru/post/975603/


All Articles