1

I want to make an iPhone app which shows a view when iPhone is in portrait mode, and ANOTHER when iPhone is in landscape mode. I know there is many post about that but I don't understant the answer.

In a first time, to understand, I make test with a Tabbed Application, because I have already two views. When I tap on the second screen, I would like my iphone in landscape mode. (and in the first one in portrait mode).

On Apple website, and stackoverflow, I saw the following code :

- (NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskLandscapeLeft;
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
return UIInterfaceOrientationLandscapeLeft;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)orientation
{
   if ((orientation == UIInterfaceOrientationPortrait) ||
   (orientation == UIInterfaceOrientationLandscapeLeft))
  return YES;

   return NO;
}

Or a similar code.

In my mainstoryboard, I put the second view in landscape, with the interface.

But when I run my app, and I tap on second screen, iPhone stay in portrait mode..

I tried to do the same thing with a single view app, and created new file (landscapeViewController) with .xib file, but I can't have a godd result!

rmaddy
  • 314,917
  • 42
  • 532
  • 579
papay0
  • 1,311
  • 1
  • 12
  • 25
  • you can try to switch the mode programmatically: http://stackoverflow.com/questions/10670834/changing-device-orientation-to-landscape-programmatically-doesnt-work – Eugene Gordin May 13 '14 at 22:48
  • This methode `[[UIDevice currentDevice] setOrientation: UIDeviceOrientationLandscapeLeft];` doesn't work in iOS7 (read only) And it is the same for this code : `if (UIDeviceOrientationIsPortrait([[UIDevice currentDevice] orientation])) { UIWindow *window = [[UIApplication sharedApplication] keyWindow]; UIView *view = [window.subviews objectAtIndex:0]; [view removeFromSuperview]; [window addSubview:view]; }` (very curious code ...) – papay0 May 13 '14 at 23:15
  • try this then :) http://stackoverflow.com/questions/20987249/how-do-i-programmatically-set-device-orientation-in-ios7 – Eugene Gordin May 13 '14 at 23:24

2 Answers2

1

First, in storyboard, create segues from your portrait view controller to your landscape view controller and vice-versa. Then, in your portrait view controller, do this:

- (void)viewWillLayoutSubviews
{
    if (UIInterfaceOrientationIsLandscape(self.interfaceOrientation)) {
        [self performSegueWithIdentifier:@"SegueToLandscapeViewController" sender:self];
    }
}

In your landscape view controller, do this:

- (void)viewWillLayoutSubviews
{
    if (!UIInterfaceOrientationIsLandscape(self.interfaceOrientation)) {
        [self performSegueWithIdentifier:@"SegueToPortraitViewController" sender:self];
    }
}
alanb
  • 21
  • 3
  • Why must I create segues if I want force the landscape mode in a view ? – papay0 May 14 '14 at 10:51
  • Segues are used to switch from your portrait view controller to your landscape view controller and back again, as your user rotates his or her iOS device. If you want one of your tabs to appear as portrait and the other to appear as landscape, regardless of device orientation, I don't know if that's possible. Is that really what you want? Or were you just using tabs as a convenient way to switch between your view controllers? – alanb May 14 '14 at 17:43
0

I succeeded with the following code :

-(void)orientationDidChanged: (NSNotification *)notification {

UIDeviceOrientation devOrientation = [UIDevice currentDevice].orientation;

if (UIDeviceOrientationIsLandscape(devOrientation)) {

    UIStoryboard *main = [UIStoryboard storyboardWithName:@"Main" bundle:nil];

    landscapeViewController *landscape = [main instantiateViewControllerWithIdentifier:@"landscape"];

    [self presentViewController:landscape animated:YES completion:nil];

}

else if (UIDeviceOrientationIsPortrait(devOrientation)) {

    [self dismissViewControllerAnimated:YES completion:nil];   
}
}

But I can't change transition between View Controller, whereas I can between simple View.

Maybe it's because I have two views controllers linked to the same view controller.h...?

papay0
  • 1,311
  • 1
  • 12
  • 25