11

I want to set UIViewController in Portrait, I tried to add

- (UIInterfaceOrientation) preferredInterfaceOrientationForPresentation {
    return UIInterfaceOrientationPortrait;
}

- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
    return UIInterfaceOrientationMaskPortrait;
}

- (BOOL)shouldAutorotate {
    return [UIApplication sharedApplication].statusBarOrientation != UIDeviceOrientationPortrait;
} 

But when I take my phone in Landscape, then enter this UIViewController,

I will run into Landscape first, then rotate to portrait!

but I don't want it shows landscape, I want it show portrait immediate,

Is there any wrong setting?

EDIT

I called the error ViewController "vcA", it present from "vcB"

in vcB's viewDidAppear, I called

[self presentViewController:vc animated:NO completion:nil];

then vcA will show in landscape(if vcB was in landscape),

if add a few delay like:

dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
    [self presentViewController:vc animated:NO completion:nil];
});

it will show portrait, but I do not know why it happen!

ANSWER

Finally, I found root cause!

  1. when I launch app, it stay in Portrait mode,

  2. After launch, vcB is first VC I called,

  3. Phone stay in landscape, so when viewDidAppear in vcB, it call rotate to Landscape

  4. At same time in viewDidAppear, I also call present:vcA

  5. So currently, view have to do both rotate to landscape and present vcA(should in Portrait!!)

  6. Then the bad situation happened!

Solution

Just disable rotate for first launch in "vcB", then in viewDidAppear, it will only call present:vcA

Thanks all a lot!

Devin
  • 286
  • 1
  • 5
  • 14
  • have you tried using viewWillAppear to force the view to be portrait? – jacob bullock Jul 25 '16 at 09:45
  • Yes, I've tried. It can rotate to portrait correctly, But it also show in landscape first, how can I disable landscape view? I don want to show landscape, it's not supported! – Devin Jul 25 '16 at 09:56
  • @Devin check this answer maybe can help you https://stackoverflow.com/questions/38308919/unable-to-force-uiviewcontroller-orientation/38308987#38308987 – Reinier Melian Jul 25 '16 at 18:25
  • Finally, I found the answer,~thanks a lot – Devin Jul 26 '16 at 04:02

3 Answers3

12
  1. Go to General settings of your xcode project

  2. Find deployment info

  3. Check the device orientation to portrait only.

    enter image description here

OR

- (void)viewWillAppear:(BOOL)animated {

   [[UIDevice currentDevice] setValue:[NSNumber numberWithInteger: UIInterfaceOrientationPortrait]forKey:@"orientation"];
 }

- (BOOL)shouldAutorotate{
    return NO;
 }
Akash KR
  • 778
  • 3
  • 11
  • I only want to set portrait in current viewcontroller, change in General is no appropriate to me, I try to add function in viewWillAppear, but it still show landscape first! – Devin Jul 25 '16 at 09:58
  • Yes, I also tried this! It will stop in landscape. I found something may make it happen, I'll add it soon! – Devin Jul 25 '16 at 10:04
6
  1. Select your project->Target->Project->General
  2. Check and uncheck according to the screenshot(Under Deployment)
  3. Make sure you check requires full screen or else you can not submit your app to app store.

enter image description here

Rokon
  • 355
  • 3
  • 11
4

In Xcode version 13.2.1

and project with SceneDelegate,

the situation is not as web usual,

class AppDelegate{
    func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {
        return UIInterfaceOrientationMask.portrait
    }
}

it works


The following not works well

44

dengST30
  • 3,643
  • 24
  • 25