I have a view which is presented as full screen. I want the presented full screen view to be restricted just to portrait.
Can anyone help me out with restricting only one view as portrait? It should not turn to landscape.
I have a view which is presented as full screen. I want the presented full screen view to be restricted just to portrait.
Can anyone help me out with restricting only one view as portrait? It should not turn to landscape.
The code you use depends on the iOS you are targeting:
There's a method called supportedInterfaceOrientations
that does what you are looking for:
- (NSUInteger)supportedInterfaceOrientations
{
//If you want to support landscape
return UIInterfaceOrientationMaskAll;
//If you don't
return UIInterfaceOrientationMaskPortrait;
}
- (BOOL)shouldAutorotate {
return YES;
}
Put the according return statement in each view controller.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation {
return !(UIInterfaceOrientationIsLandscape(toInterfaceOrientation));
}
If you are using the model presenting between VCs, @David 's answer is correct.
However if you are using UINavigationController
push/pop, things will be more complicated.
- (NSUInteger)supportedInterfaceOrientations
and - (BOOL)shouldAutorotate
only be called after device rotated or root controller changed. push/pop will not make it.
In iOS5 and prior, there is an API:
[[UIDevice currentDevice] setOrientation: UIInterfaceOrientationPortrait];
But it was deprecated. Apple make it private.
You still can use objc runtime api to call it in iOS 6+, such as:
objc_msgSend([UIDevice currentDevice], @selector(setOrientation:), @(UIInterfaceOrientationPortrait));
or
[[UIDevice currentDevice] setValue:@(UIInterfaceOrientationPortrait)
forKey:@"orientation"];
NOTE: it's private API, This may be rejected by the App Store.
Another tricky way is that, just present a empty VC then dismiss it in viewDidAppear
, something like:
[self presentViewController:[UIViewController new]
animated:NO
completion:^{
[self dismissViewControllerAnimated:NO completion:nil];
}];
This will make the - (NSUInteger)supportedInterfaceOrientations
and - (BOOL)shouldAutorotate
to be called.
If you are not satisfied with those above. Try to make your own NavigationController, or check out this Question: Why can't I force landscape orientation when use UINavigationController?.