IOS7/IOS8 Allow only portrait in view controller
Asked Answered
L

2

2

I am building an app for iPhone that will have only 1 landscape view, and so i want to block landscape for all other, i have tried this:

-(NSUInteger)supportedInterfaceOrientations
{
   return UIInterfaceOrientationMaskPortrait;
}

But it stills rotates

Loggia answered 28/10, 2014 at 15:4 Comment(4)
why not just setting the app for only portrait mode and then whenever you need the landscape mode just allow landscape mode?Gnostic
@NorthBlast I don't think you can do that. If you only enable portrait in the project then you don't get notified of the orientation changes at all.Hamite
I have done it several times.. :) and my project is set to portrait mode.. known as forcing landscape mode ;)Gnostic
probably you need to create an own subset of your navigation controller class, I assume you are using a standard UINavigationController, so then you need to override that class's –shouldAutorotate and –supportedInterfaceOrientations methods especially in your subset.Christiechristin
G
9

I will suggest to just make your app for portrait mode and then whenever you need the landscape mode then allow landscape mode.

First, as previously suggested click on -> Project name -> General -> Deployment Info -> Only select Portrait for Device Orientation.

Second, in your AppDelegate.h add this property..

@property (nonatomic) BOOL fullScreenVideoIsPlaying;

Then, on your AppDelegate.m I will add this function..

- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window{
    if (self.fullScreenVideoIsPlaying == YES) {
        return UIInterfaceOrientationMaskAllButUpsideDown;
    }
    else {
        return UIInterfaceOrientationMaskPortrait;
    }
}

After doing this, in the view controller that you need landscape create a function or just add this code to your viewWillAppear method is depending how you want to accomplish this..

((AppDelegate *)[[UIApplication sharedApplication] delegate]).fullScreenVideoIsPlaying = YES;
[[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationPortrait animated:NO];

Then for setting back to portrait mode you do this..

AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    appDelegate.fullScreenVideoIsPlaying = NO;

[self supportedInterfaceOrientations];

[self shouldAutorotate:UIInterfaceOrientationPortrait];

[[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationPortrait animated:NO];

You might need these functions for iOS 8..

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

- (BOOL)shouldAutorotate:(UIInterfaceOrientation)interfaceOrientation{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

-(NSUInteger)supportedInterfaceOrientations{
    return UIInterfaceOrientationMaskPortrait;
}

I hope it helps.. :)

Gnostic answered 28/10, 2014 at 15:52 Comment(1)
What if [self presentMoviePlayerViewControllerAnimated:self.player]; ?Hammerskjold
C
-3

Click project and select Orientation settings from there.

Claudetta answered 28/10, 2014 at 15:15 Comment(2)
And what about the landscape View that i need?Loggia
Actually, it does provide an answer. That answer may not be correct, but it is an answer.Lingwood

© 2022 - 2024 — McMap. All rights reserved.