質問

This topic has come up before (iPad modal view controller acting in portrait even though it's landscape) but I haven't found a clear answer - so I don't know if this is a duplicate or not.

In new single view project, I set the main view to landscape in xcode:

enter image description here

And the Property Inspector confirms this (as well as how the view is displayed in the storyboard):

enter image description here

And the ViewController orientation property is set to landscape:

enter image description here

Yet when I check the view frame in 'viewDidLoad' it reports portrait mode:

- (void)viewDidLoad
{
    [super viewDidLoad];

    CGRect theRect = self.view.frame;

    NSLog(@" frame %f  %f  %f  %f", theRect.origin.x,
          theRect.origin.y,
          theRect.size.width,
          theRect.size.height);
}

2012-08-26 16:42:45.045 Test[2320:f803] cell 0.000000 20.000000 768.000000 1004.000000

I also force landscape in shouldAutorotateToInterfaceOrientation:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return UIInterfaceOrientationIsLandscape(interfaceOrientation);
}

I've encountered this many times before and have had to set the frame explicitly to landscape but I have never understood why all the storyboard settings have no effect.

Am I missing something basic here?

役に立ちましたか?

解決

Every application in iOS starts in portrait mode inititally, even if you specified the supported device orientations and give the right "answers" at shouldAutorotateToInterfaceOrientation:. It will always start in portrait and will the rotate to landscape if the device. The user maybe won't see it cause its so fast. Because of this your app has to be able to rotate via shouldAutorotateToInterfaceOrientation even if your only supported orientations are landscape ones.

So to get a landscape orientation after start you should:

  • set the supported interface orientations in Xcodes Interface Builder
  • overide shouldAutorotateToInterfaceOrientation:

- (BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)io {
    return (io == UIInterfaceOrientationLandscapeRight); 
}
  • give the interface a chance to rotate and do your view configuration afterwards

Regarding your question about the Xcode configuration of the the viewcontroller to landscape: Notice the title of the menu in the storyboard - it says: Simulated Metrics
This means that every modification you do there is just for the purpose to simulate it in the storyboard. But unless you do the necessary modifications to get to this state in the code it will have no effect.

他のヒント

Add below code in your view controller with swift 4.2

override var shouldAutorotate: Bool {
    return true
}

override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
    return .landscapeRight
}
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top