1
votes

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!

2
you can try to switch the mode programmatically: stackoverflow.com/questions/10670834/…Eugene Gordin
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

2 Answers

1
votes

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];
    }
}
0
votes

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...?