1

I suppose this is a more basic question. I am stuck with this app I am coding for the iPhone X. I have it set to display a lot of buttons on the Main.storyboard file. Everything looks how I want to look but the problem is I do not know how to make the screen change with landscape rotation left and landscape rotation right. The screens looks the same but I want to style the page differently.

Xcode 9.1 iOS 11 Swift 4.03

rmaddy
  • 314,917
  • 42
  • 532
  • 579
  • You'll need to do two things in code: (1) Detect if it's landscape left or landscape right - (https://stackoverflow.com/questions/38629782/how-to-check-if-device-orientation-is-landscape-left-or-right-in-swift - and then (2) alter the layout accordingly *in code*. NOTES: In the link I provided a comment mentions the known issue of a device laying on a flat surface. Also, I know of no way to design separate layouts in IB, where "landscape" is just "landscape". –  Dec 30 '17 at 18:37
  • Take a look at this documentation from Apple: https://developer.apple.com/library/content/featuredarticles/ViewControllerPGforiPhoneOS/TheAdaptiveModel.html – Kevin Stewart Dec 30 '17 at 19:32

3 Answers3

1

If you want a an actually different layout for landscape (and not simply an adaptive one) you should use size classes: https://developer.apple.com/library/content/documentation/UserExperience/Conceptual/AutolayoutPG/Size-ClassSpecificLayout.html

This will enable you to make completely different UI for different orientations.

Interface Builder lets you customize many of your layout’s features based on the current size class. The layout then automatically adapts as the size class changes.

...

As the view’s size class changes (for example, when you rotate an iPhone or switch an iPad app between full-screen and Split View), the system automatically adds items to or removes them from the view hierarchy. The system also animates any changes to the view’s layout.

pajevic
  • 4,607
  • 4
  • 39
  • 73
  • Like a (now deleted) answer - the OP wants to know about landscape **left** and landscape **right**. Size classes do not do this. –  Dec 30 '17 at 22:46
  • I see. I assumed that he just mentioned left and right to cover his bases. But I see now that you are right. – pajevic Dec 31 '17 at 06:31
1

If the only reason you want different layouts in landscape right and landscape left is to avoid the “notch” at the top of the screen and the gesture area at the bottom, then what you really should be doing is laying out your views relative to the “safe area”, which exists for just this reason. If your views are laid out relative to the safe area layout guides, then they’ll automatically adjust to avoid the notch and the gesture area automatically, regardless of orientation.

https://developer.apple.com/documentation/uikit/uiview/positioning_content_relative_to_the_safe_area

Mark Bessey
  • 19,598
  • 4
  • 47
  • 69
0

You could make two xib files for each specific orientation and then using what user dfd suggested, load the xibs respectively in the following code:

if UIDevice.current.orientation == UIDeviceOrientation.landscapeLeft {
    //Load xib for landscapeLeft
} else if UIDevice.current.orientation == UIDeviceOrientation.landscapeRight
    //Load xib for landscapeRight
{

This would allow you to style the orientations differently.
Good Luck,
Arnav


Source:
https://stackoverflow.com/a/38629833/8503080

  • 1
    I would recommend what Mark Bessey answered if you're worried about the notch. Using the safe area to avoid the notch is the best and the right way to design your views. My answer is if you want different views for different orientations. Hope that helps. – Arnav Kaushik Dec 31 '17 at 04:08