Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to set orientation in Portrait only?

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!

like image 432
Devin Avatar asked Jul 25 '16 09:07

Devin


2 Answers

  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;
 }
like image 84
Akash KR Avatar answered Oct 18 '22 02:10

Akash KR


  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

like image 37
Rokon Avatar answered Oct 18 '22 02:10

Rokon