2

Is there a way to show the home indicator in its portrait position even if the screen orientation is landscape?

hasan
  • 23,815
  • 10
  • 63
  • 101
Mitul Jindal
  • 620
  • 4
  • 16

2 Answers2

2

As @the4kman pointed out, the only way to achieve this, is by using view transformation:

view.transform = CGAffineTransform(rotationAngle: .pi / 2)

But also, the view that should be transformed should be an inner view. which you need to set its height equal to the superview width and its width equal to the superview height. And it should be centered vertically and horizontally.

enter image description here

You can support all orientations in your app and only disable it for that specific controller.

The view controller implementation:

import UIKit

class PortraitController: UIViewController {

    @IBOutlet weak var viewLandscaped: UIView!

    override func viewDidLoad() {
        super.viewDidLoad()

        viewLandscaped.transform = CGAffineTransform(rotationAngle: .pi / 2)
    }

    override var supportedInterfaceOrientations : UIInterfaceOrientationMask {
        return .portrait
    }
}

Results:

enter image description here enter image description here

hasan
  • 23,815
  • 10
  • 63
  • 101
  • I work with Mitul. Supporting different orientations for different view controllers causes a different problem: https://stackoverflow.com/questions/42743526/can-i-transition-between-view-controllers-without-a-rotation-animation – Kartick Vaddadi Oct 25 '18 at 03:05
1

You cannot modify the position of the home indicator. It would be also bad UX, since the user expects it to be on the bottom.

A workaround for this would be rotating the superview with all your UI elements with CGAffineTransform. For this to work, you should only allow the portrait orientation in your app (or in a particular view controller), so that the user interface would display in landscape at all times.

view.transform = CGAffineTransform(rotationAngle: .pi / 2)

...or for landscape left:

view.transform = CGAffineTransform(rotationAngle: .pi / -2)

Also, keep in mind that your rotating superview should flip its view and height, so that it would fill the whole screen. For example, you should resize it from 320x568 to 568x320.

Tamás Sengel
  • 55,884
  • 29
  • 169
  • 223
  • 1
    The transformation by itself is not enough. and it should be done on the superview. also, you need to set the width of the transformed view to the height of the screen loading the data. can you include that in your answer. I upvoted already – hasan Jul 03 '18 at 12:26
  • I tried this. This only works if I remove all other orientations from the app. I want this to happen only in a particular UIViewController. – Mitul Jindal Jul 03 '18 at 12:26
  • 1
    this also need to be corrected on the answer. you don't have to only support the portrait orientation you can support all of them. but for that specific controller you can disable the landscape orientation – hasan Jul 03 '18 at 12:27
  • It should work. try to open another question and include the codes on it. – hasan Jul 03 '18 at 12:31
  • I know that code, it doesn't work. I have overridden supportedInterfaceOrientations to portrait only. – Mitul Jindal Jul 03 '18 at 12:33
  • so your view doesn't flip but the indicator does? – hasan Jul 03 '18 at 12:36
  • I've updated my answer with some links to useful questions. – Tamás Sengel Jul 03 '18 at 12:42
  • I tried it. it works. you don't need to only allow the portrait orientation. – hasan Jul 03 '18 at 12:45
  • I work with Mitul. @hasan Yes, currently the view doesn't flip, but the home indicator keeps moving around to all four corners of the screen. – Kartick Vaddadi Oct 25 '18 at 03:07
  • @TamásSengel Supporting different orientations for different view controllers causes a different problem: https://stackoverflow.com/questions/42743526/can-i-transition-between-view-controllers-without-a-rotation-animation – Kartick Vaddadi Oct 25 '18 at 03:07