1

I am starting on first project in swift and I need to add a new ViewController when the screen is rotated in landscape mode. And if rotated back to portrait mode it will return to original screen. I am a beginner in swift so please make explanation as clear as possible. Thank you in advance!

Joe
  • 11
  • 2
  • Could you please add more information in which ways shall it change? Should extra window appear, or shall it replace original ViewController? And what device are we talking about? Thanks) – Bolat Tleubayev Feb 18 '20 at 14:39
  • So lets say in portrait mode I will only have one containerview but when the user rotates the device to landscape an extra containerview will be added to landscape mode. And when the user rotates back to portrait it goes back to one containerview. – Joe Feb 18 '20 at 14:48
  • You can use Size Classes. – Don Feb 18 '20 at 14:53

2 Answers2

0

It seems that you should definitely check out SplitViewControllers. It allows you (for iPads and iPhone Pluses) have sort of a split screen with two views simultaneously (for other devices it will be just like navigation controller).

Below couple of screenshots of a dummy SplitView I just did

Here is the Landscape mode

This is in a Landscape mode

Here is the Portrait mode

Here is the Portrait mode

This is in a Portrait mode

You can also make it collapsable by tracking the orientation of the device or gestures. By default, you can show or collapse detail view by left/right swipe gesture.

Here you can check out a decent tutorial on SplitViewController

Good luck!

Bolat Tleubayev
  • 1,765
  • 3
  • 14
  • 16
  • So i tried using splitview but it could not get it to work because I need more than a table for the master view table. Is there anyway to add more than a table to the masterview? – Joe Feb 18 '20 at 14:58
  • @Joe what do you mean by more than a TableView? You need to have a TableView as a master? – Bolat Tleubayev Feb 18 '20 at 15:02
  • So if you take a look at the picture below at the link. So my goal is to add a calender to the left side on the root controller view. And on the right side is a drawing pad. The user will be able to save drawings to each date. How can I allow the splitview to let me import a calender that is more than just a normal table view.https://i.stack.imgur.com/7APst.jpg – Joe Feb 18 '20 at 15:13
  • It seems that there is no special calendar view in swift (if you want one, you may need to create it), so what appears to me is that you will need to replace your TableViewController from detail and put a custom Calendar (probably done in ScrollView) instead – Bolat Tleubayev Feb 18 '20 at 15:15
0

Try this

First ViewController

class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {


override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
    super.viewWillTransition(to: size, with: coordinator)
    if UIDevice.current.orientation.isLandscape {
        print("Landscape")
        let vc2 = self.storyboard?.instantiateViewController(withIdentifier: "ViewController2")
        self.navigationController?.pushViewController(vc2!, animated: false)
    } else {
        print("Portrait")
    }
}
override func viewDidAppear(_ animated: Bool) {
    let value = UIInterfaceOrientation.portrait.rawValue
    UIDevice.current.setValue(value, forKey: "orientation")
}
override func viewDidLoad() {
    super.viewDidLoad()
}
}

Second ViewController

class ViewController2: UIViewController {

override func viewDidLoad() {
    super.viewDidLoad()

    // Do any additional setup after loading the view.
}

override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
    super.viewWillTransition(to: size, with: coordinator)
    if UIDevice.current.orientation.isLandscape {
        print("Landscape")
    } else {
        print("Portrait")
        self.navigationController?.popViewController(animated: false)
    }
}
}

You will detect orientation change in viewWillTransition method, so you need to override it