Nelson Canino
Nelson Canino

Reputation: 35

How do I make my app display another screen for landscape left and landscape right, iPhone X iOS

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

Upvotes: 1

Views: 398

Answers (3)

Arnav Kaushik
Arnav Kaushik

Reputation: 45

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

Upvotes: 0

Mark Bessey
Mark Bessey

Reputation: 19782

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

Upvotes: 1

pajevic
pajevic

Reputation: 4657

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.

Upvotes: 1

Related Questions