1

I am making an Application in which i need to show one view in portrait orientation and a completely different view in landscape mode (like the stocks app on iPhone). For example if I made a weather app, in portrait I would want to show current weather, and in landscape show a graph of that weather. How would I accomplish this, would I have to make a different storyboard and change the size class?

fellowProgrammer
  • 327
  • 3
  • 17

2 Answers2

1

It's called adaptive layout. Check out this nice tutorial on Ray Wenderlich. https://www.raywenderlich.com/113768/adaptive-layout-tutorial-in-ios-9-getting-started

Bhavuk Jain
  • 2,167
  • 1
  • 15
  • 23
1

Adaptive layout won't necessarily cut it. My app needed something similar - and in iPad using AutoLayout, you have no clue if you are using size classes. You need to measure the superview width and height.

I was very gratified when Apple even did did what I did at WWDC 2016 ("Making Apps Adaptive, Part 2"). They dropped into code (IB, while close, won't cut it 100%) with:

override func viewWillLayoutSubviews() {
    super.viewDidLayoutSubviews()
    if self.bounds.width = self.bounds.height {
        // view is in landscape mode
    } else {
        // view is in portrait mode
    }
}

This is the key thing to do. From there it depends on your needs. My needs were pretty simple, move some views to the right side instead of below the image view if it's landscape. Apple's Stock app looks like it's also setting isHidden to red on things, which is easily done with AutoLayout.

  • I am not trying to change the constraints or move objects around the screen when the orientation is changed. Basically what do is change the view completely. Let's say I was making a weather app, in portrait orientation I would want to show the current weather, and in landscape I would want to show a graph. How would I do this? @dfd – fellowProgrammer Oct 29 '16 at 12:57
  • I may be misunderstanding you. In my eyes right now, it's still the same concept. "Changing the view completely" involves hiding the current view and "unhiding" a hidden view, which is either moving subviews around or - and I wouldn't recommend this - segueing between two views (and controllers). –  Oct 29 '16 at 16:05
  • Okay, just one more question how would I make a UIView the size of the screen put my labels in it (programitacally) and then hide that view and show a different one. @dfd – fellowProgrammer Oct 29 '16 at 19:36
  • Use AutoLayout and isHidden. I'm still learning what I can and cannot do in comments here, so let me post an answer. Hope it helps! –  Oct 30 '16 at 02:46