I've see a dozen of possible related questions on SO, but no one seems duplicated:
how to force view to rotate to landscape in viewdidload?
force landscape ios 7
IOS - How to force the view to landscape
Force landscape for one view controller ios
iOS - Force Landscape Orientation
How to make app fully working correctly for autorotation in iOS 6?
My app is a tab-based application.
Currently when entering certain views, I can rotate my phone to landscape to let my view enter landscape.
Main Code:
// In app delegate
- (UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window {
if (_allowRotation == YES) {
return UIInterfaceOrientationMaskLandscape;
}
else {
return UIInterfaceOrientationMaskPortrait;
}
}
In views that I want to be landscape, I set allowRotation to YES.
But I want to force views to enter landscape.
In other word, when entering certain views, it automatically enters landscape, no need to rotate phone. Even user locks the screen as portrait. How to achieve this?
I'm using iOS 10.
You have to override the onDidLoad or onDidAppear of the specific view that you want to move it to landscape, for example i added it to viewWillAppear and Disappear:
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:YES];
self.navigationController.navigationBarHidden = NO;
NSNumber *value = [NSNumber numberWithInt:UIInterfaceOrientationLandscapeRight];
[[UIDevice currentDevice] setValue:value forKey:#"orientation"];
}
-(void)viewWillDisappear:(BOOL)animated {
NSNumber *value = [NSNumber numberWithInt:UIInterfaceOrientationPortrait];
[[UIDevice currentDevice] setValue:value forKey:#"orientation"];
[super viewWillDisappear:animated];
}
so we the view appear the application move to landscape and when the move disappear it come again to portrait. and for the appDelegate you have to add this:
- (UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
{
UINavigationController *navigationController = (UINavigationController *)self.window.rootViewController;
if ([navigationController.viewControllers.lastObject isKindOfClass:[OpencvViewController class]])
{
return UIInterfaceOrientationMaskLandscape;
}
else
return UIInterfaceOrientationMaskPortrait;
}
Call [UIViewController attemptRotationToDeviceOrientation] when you need rotate orientation
Yours code will be something like that:
appDelegate.allowRotation = YES;
[UIViewController attemptRotationToDeviceOrientation];
Use below code in the specific view controller that you want to rotate in to landscape mode.
-(void)viewDidLayoutSubviews
{
NSNumber *value = [NSNumber numberWithInt:UIInterfaceOrientationLandscapeLeft];
[[UIDevice currentDevice] setValue:value forKey:#"orientation"];
}
You need to use
- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
return UIInterfaceOrientationMaskLandscape;
}
for container view controller in window.rootViewController hierarchy or while presenting view controller over another one.
This enforces you controller to appear in landscape orientation.
The simplest example is assigning rootViewController to the main window.
If you use UIInterfaceOrientationMaskLandscape in your view controller it appears in landscape from the very beginning.
If you try to pack your controller into navigation controller you'll need to subclass the latter to define UIInterfaceOrientationMaskLandscape in it because it is the container for your own view controller.
The same is valid for other containers (e.g. tabBarController or your own containers) which have parent-child link.
While presenting view controller you have also another method to fix orientation:
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
Related
I have an app that is portait mode only. I only have one View Controller that I need to force into landscape mode (A customer signature view) so there is more room for the customer to sign.
How can I force one ViewController to appear in landscape mode. Leaving the rest in portrait?
I have tried setting the app's General Deployment info to Portrait, Landscape Left
And then using this code on the Signature View
- (BOOL)shouldAutorotate {
return YES;
}
and returning NO on all other view controllers. But that didn't seem to work.
I also included
-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return (interfaceOrientation == UIInterfaceOrientationLandscapeLeft);
}
Is this possible?
Try this code in the Signature View Controller
- (void)viewDidLoad
{
[super viewDidLoad];
[[UIDevice currentDevice] setValue:[NSNumber numberWithInteger:UIInterfaceOrientationLandscapeLeft] forKey:#"orientation"];
}
-(void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
[[UIDevice currentDevice] setValue:[NSNumber numberWithInteger:UIInterfaceOrientationPortrait] forKey:#"orientation"];
}
You're just missing the following code below, if your UIViewController is a rootcontroller of a navigationcontroller you need to put the code in the navigationcontroller. Hope this helps, good luck.
- (UIInterfaceOrientationMask)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskLandscape;
}
Following is the the way its working for me:-
-(UIInterfaceOrientationMask)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskLandscapeLeft;
}
and
- (BOOL)shouldAutorotate {
return NO;
}
Another way is to define orientation in prepareForSegue and fixed orientation of the required VC and set shouldAutorotate to NO in that VC.
Let me know if mentioned code doesn't work for you.
I have an app which has a rootViewController and several child views. I want to lock the screen orientation to portrait for all of the views except one. The one view is an UIPageViewController which display's several web views and it needs to be landscape.
I can force it into landscape mode with the following code in my viewDidLoad:
NSNumber *value = [NSNumber numberWithInt:UIDeviceOrientationLandscapeLeft];
[[UIDevice currentDevice] setValue:value forKey:#"orientation"];
I can't seem to prevent the view from transitioning back to portrait on rotation. I need to lock it into landscape mode.
The following code does not seem to prevent rotation:
- (BOOL)shouldAutorotate {
return NO;
}
- (NSUInteger)supportedInterfaceOrientations {
return (UIInterfaceOrientationMaskLandscape);
}
- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window{
return UIInterfaceOrientationLandscapeLeft;
}
I'm targeting 7.1 and up. What am I missing?
My iOS app supports both landscape and portrait. The splash screen, login view and the home view are all portrait.
Now if I rotate the device to landscape and start the app all three views are and stay in portrait which is what is needed. But if I start the app in portrait and during splash screen I rotate the device to landscape the login screen is shown in landscape then automatically moves to portrait. Same for home screen (auto login, when user has already logged in and reopened the app, it goes directly to home screen).
I used this
[[UIDevice currentDevice] setValue:[NSNumber numberWithInt:UIInterfaceOrientationPortrait] forKey:#"orientation"];
in viewDidLoad, viewDidAppear, viewWillAppear and viewWillDisappear. I also used
-(NSUInteger)supportedInterfaceOrientations{
return UIInterfaceOrientationMaskPortrait;
}
So, what I want to achieve is to keep the views in portrait even if I rotate the device during the splash screen.
Thank you.
Try putting this in your viewDidLoad call:
// Change to portrait if in landscape mode
if (UIDeviceOrientationIsLandscape([[UIDevice currentDevice] orientation])) {
NSNumber *value = [NSNumber numberWithInt:UIInterfaceOrientationPortrait];
[[UIDevice currentDevice] setValue:value forKey:#"orientation"];
}
And remember to call super in supportedInterfaceOrientations
-(NSUInteger)supportedInterfaceOrientations{
[super supportedInterfaceOrientations];
return UIInterfaceOrientationMaskPortrait;
}
Hope that helps.
You should override your viewController's shouldAutorotate method and return there NO in those controllers, which you don't want to rotate.
EDIT: Try comment-out the shouldAutorotate method and implement
- (NSUInteger)supportedInterfaceOrientations {
return UIInterfaceOrientationMaskPortrait;
}
There was a problem with the flow of the app that was causing the problem. The solutions provided by Ryan and Soberman are helpful. These lines of code should work fine
supportedInterfaceOrientations
NSNumber *value = [NSNumber numberWithInt:UIInterfaceOrientationPortrait];
[[UIDevice currentDevice] setValue:value forKey:#"orientation"];
I have 2 view controllers A and B. View controller A is portrait only and view controller B can rotate.
When the phone is landscape, I push view controller A and it's orientation is portrait -> good.
But when i push view controller B from view controller A (the phone is still landscape), view controller B's orientation is portrait, that is not what i want.
I have to rotate device to portrait then landscape to make view controller B rotate to landscape.
My question is, how can i make view controller B rotate immediately after push from A, in respect to device orientation?
Use this it may be resolve your poblem
AppDelegate.m
-(NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
{
return UIInterfaceOrientationMaskLandscape;
}
ViewController
-(void) restrictRotation:(BOOL) restriction
{
AppDelegate* appDelegate = (AppDelegate*)[UIApplication sharedApplication].delegate;
appDelegate.restrictRotation = restriction;
}
To rotate device orientation use this code in your viewWillAppear and viewWillDisAppear according to need
NSNumber *value = [NSNumber numberWithInt:UIInterfaceOrientationPortrait];
[[UIDevice currentDevice] setValue:value forKey:#"orientation"];
And to rotate View use this code
[self shouldAutorotate];
[self supportedInterfaceOrientations];
And Define this method in ViewController.m
-(BOOL)shouldAutorotate{
return YES;
}
-(NSUInteger)supportedInterfaceOrientations{
return UIInterfaceOrientationMaskPortrait;
}
It will Help.Thankyou
Okay, so here's the situation:
I have an app in which I only want ONE specific view in a UINavigationController to have a landscape orientation. This view is a UIImageView that I'm capturing a signature on (THAT part works awesome). So, like this:
previous view --> signature view --> next view
(portrait) (landscape) (portrait)
I can't seem to find a good way to force the device orientation to landscape on that signature screen. It'll never make sense to have a portrait orientation on the signature view because there's really not adequate room for signing in that screen width.
So, any bright ideas on how to accomplish this? I've considered possibly doing the signature view modally, thus breaking out of the navigation controller. Thoughts?
You can try to force Device to rotate to necessary orientation - but you need to handle it manually (in addition to overriding UIViewController orientation handling methods).
To rotate device you can use next methods:
[[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeRight];
But in it may not work in all situations...
Also available undocumented approach:
[[UIDevice currentDevice] performSelector:NSSelectorFromString(#"setOrientation:")
withObject:(__bridge id)((void*)UIInterfaceOrientationLandscapeLeft)];
Just override this UIViewController method to only return true for landscape like so and the iphone will be forced to rotate to that device orientation, since it has no other option.
- (BOOL)shouldAutorotateToInterfaceOrientation: (UIInterfaceOrientation)interfaceOrientation {
return (interfaceOrientation == UIInterfaceOrientationLandscapeRight);
}
Unfortunately, all root UIViewControllers inside of a Navigation Controller must support any of their child orientations. This means that the first view controller in your setup must support landscape, otherwise the child will only support portrait.
The best way to implement what you are looking for is to create a UIViewController that displays its content view on a rotated transform, and just default all UIViewControllers in that stack to portrait.
I think you can embed this view inside a view controller and overwrite the ShouldRotateToInterfaceOrientation method.
Good luck!
To use a View in only landscape, I have the following in the ViewController:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationLandscapeRight || interfaceOrientation == UIInterfaceOrientationLandscapeLeft);
}
This might be what your looking for.
// Rotates the view.
CGAffineTransform transform = CGAffineTransformMakeRotation(3.14159/2);
self.view.transform = transform;
// Repositions and resizes the view.
CGRect contentRect = CGRectMake(-80, 80, 480, 320);
self.view.bounds = contentRect;
from http://www.iphonedevsdk.com/forum/iphone-sdk-development/1394-landscape-uiviewcontroller-uiview-rotation.html
I have an app that has landscape only views that even starts in landscape. This was working fine in iOS 5.x but stopped working in iOS 6.x
After trying many many things, some more questionable than others, I found a solution that to me is clear and predictable.
I did several things.
-- I kept the views in landscape mode in IB.
-- I checked both landscape modes in the project settings - there a four icons there to control it
-- Orientation mgmt has changed in iOS 6.x. I had to overwrite a few methods to support changing to landscape
this method is for iOS 5.x
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations.
return (interfaceOrientation & UIInterfaceOrientationMaskLandscape);
}
these 2 methods are for iOS 6.x
- (NSUInteger)supportedInterfaceOrientations
{
NSUInteger supportedOrientations = UIInterfaceOrientationMaskLandscape;
return supportedOrientations;
}
- (BOOL)shouldAutorotate
{
return YES;
}
-- But the key was to change the logic in the AppDelegate. Original code I had there was adding a subview (controller.view) to the window. This stopped working in iOS 6.x - I changed the call to window.setRootController. That was the final step that sealed it - it would not work without making this final change
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
//[self.window addSubview:viewController.view];
[self.window setRootViewController:viewController];
[self.window makeKeyAndVisible];
return YES;
}
The UINavigationController overrides the contain UIViewController orientation settings, so you have to create a custom subclass of UINavigationController with the following for 5.1:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
if ([[self topViewController] isKindOfClass:[SigCaptureViewController class]]) {
return UIInterfaceOrientationIsLandscape(interfaceOrientation);
} else {
return UIInterfaceOrientationIsPortrait(interfaceOrientation);
}
}
For 6.0 and above you need:
- (BOOL)shouldAutorotate
{
return YES;
}
- (NSUInteger)supportedInterfaceOrientations {
if ([[self topViewController] isKindOfClass:[EXTRASigCaptureViewController class]]) {
return UIInterfaceOrientationMaskLandscape;
} else {
return UIInterfaceOrientationMaskPortrait;
}
}
What I haven't figured out is how to make the force the UINavigationController to rotate. calling [[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeRight animated:NO] causes the status bar to rotate but doesn't cause the view to rotate.