0

In my iOS app I want some controllers to have only .Portrait mode and moreore I would like them to rotate in .Portrait mode when coming from a controller allowed to be in .Landscape mode. I thought that implementing the following method in these .Portrait only view controllers was the right way:

override func viewWillAppear(animated: Bool) {
    self.automaticallyAdjustsScrollViewInsets = true
    super.viewWillAppear(animated)
    let value = UIInterfaceOrientation.Portrait.rawValue
    UIDevice.currentDevice().setValue(value, forKey: "orientation")

}

Anyway the result I get is not correct. For example the following is a .Portrait only UITableViewController and when viewWillAppear() is called, instead of this

enter image description here

I instead get the following

enter image description here

This happens also with UIViewController. It's like the view (or the table view) is not rotated and remains in .Landscape mode. What am I missing?

SagittariusA
  • 5,289
  • 15
  • 73
  • 127

1 Answers1

0

Do you play with Auto-Layout? When I set leading edge, trailing edge everything goes right.

Another solution can be recalculate manually the textView:

override func willRotateToInterfaceOrientation(toInterfaceOrientation: UIInterfaceOrientation, duration: NSTimeInterval) {
    textView.frame = CGRect(x: textView.frame.minY, y: textView.frame.minX, width: textView.frame.height, height: textView.frame.width)
}

UPDATE

I tested my previous solution and it doesn't work, but I found another way. Actually you need to rotate it and call viewWillAppear one more time - it works for me, try this:

if orientation.isLandscape{
   let value = UIInterfaceOrientation.Portrait.rawValue
   UIDevice.currentDevice().setValue(value, forKey: "orientation")
   viewWillAppear(false)
}

No - it doesn't work for all cases...

I'm testing it like this:

  1. I'm in app,

  2. Mobile rotation to landscape, in tab bar item (where is not checked orientation). After that I tap on tab bar item where I checked orientation, and I rotate)

  3. It rotates nicely without your problem, and when I'm doing it one more time the problem shows. But when I tap on another tab bar item and come back, everything goes right - I think we should call some method, I still don't know which one.

rudald
  • 364
  • 5
  • 18
  • Yes, I used Auto-Layout and set many constraints. So, shall I implement the method you have written? – SagittariusA Mar 31 '16 at 12:59
  • Give me a second I will test this idea and I tell you if I'm right – rudald Mar 31 '16 at 13:04
  • I think that I found a solution, but you have to try, I will edit my upper code. – rudald Mar 31 '16 at 13:17
  • I was making a silly mistake...I have just tried it but I get the same result, I'm sorry but maybe it does not work for me. Have you idea what the problem could be? – SagittariusA Mar 31 '16 at 13:41
  • I was trying to find out something but I haven't found nothing interesting, I saw that when you are rotating you call method: viewWillTransitionToSize, maybe there you can do something, maybe: http://stackoverflow.com/questions/26069874/what-is-the-right-way-to-handle-orientation-changes-in-ios-8 . Good luck! – rudald Mar 31 '16 at 15:01
  • I have just notice that the problems concerns the frame of the cell, in particular its width. So I store the original assigned width (the first time `cellForRowAtIndexPath` is called) and then in `viewWillAppear()` I just called `self.tableView.reloadData()` but this time the frame is read from the stored previous one...it seems to work. What do you think? – SagittariusA Mar 31 '16 at 15:09