Force iOS app to launch in landscape mode - objective-c

I am developing an app for iOS 5, which have to run in landscape mode. My problem is that I cannot get it to flip initially.
I have tried the adding "Initial interface orientation" set to "Landscape (right home button)"
and adding the following method to my view controller:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationLandscapeRight);
}
I can wrap my head around how it is supposed to work. (I found the code here)
I am also wondering how to use the "Supported Device Orientation" available in the Xcode 4.2 project setup, it does not seem to do anything.
I have been looking around the website and have not been able to find an example that solves my problem.
Thank you.

In your application’s Info.plist file, add the UIInterfaceOrientation
key and set its value to the
landscape mode. For landscape
orientations, you can set the value
of this key to
UIInterfaceOrientationLandscapeLeft
or
UIInterfaceOrientationLandscapeRight.
Lay out your views in landscape mode and make sure that their autoresizing options are set correctly.
Override your view controller’s shouldAutorotateToInterfaceOrientation: method and return YES only for the
desired landscape orientation and NO
for portrait orientations.

use the following in appDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
[[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeRight];
also set the required orientations in
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationLandscapeRight);
}
//Here are some iOS6 apis, used to handle orientations.
- (BOOL)shouldAutorotate NS_AVAILABLE_IOS(6_0)
- (NSUInteger)supportedInterfaceOrientations - (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation

When you click on your project's name in XCode and select your target under TARGETS, under Summary tab there's a visual representation of the supported device orientation settings. That was how I managed to do it.

i think if you at the supported interface orientations in the project plist it will work. just add a new line called "Supported interface orientations" and make it of array type and add 2 items for each of teh landsape views u want. havent tested this just a quick guess

Try this this will help you, write this code in you viewdidload.
[[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeRight];

Related

Best way to Supporting Multiple Interface Orientations - iOS8

I have been trying to convert few old apps written in objC long ago in iOS4 which support multiple ViewController with different orientation.
(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
Above Method is called in app delegate from ViewController and Desire Orientation was set but seems stopped working with iOS8.
I have tried number of solution like creating Category of UINavigation controller etc, but nothing seems working with iOS8.
What is best approach to handle Multiple Orientation for both iPhone and iPad.
Furthermore, I am not using Storyboard with size classes, in plist i am only supporting portrait orientation.
Check all the orientation you want to support in device.
For that go to Project target-> Deployment info -> Device orientation-> Tick mark the desired orientation.
From iOS 6 onwards, you've to use.
- (BOOL)shouldAutorotate {
UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];
if (orientation == UIInterfaceOrientationPortrait) {
// your code for portrait mode
}
return YES;
}
Refer to shouldAutorotateToInterfaceOrientation not being called in iOS 6

Launching an app in landscape under ios6 while supporting all orientations

I have done a ton of research and am still unable to find a solution for this...
I have an app that supports all orientations. When the app is run on the iPad I want it to launch in landscape mode. The user can then rotate to portrait if they choose. Under ios5, I accomplished this by setting supported orientations for the iPad in the info.plist file to lansdcape left. I then used the shouldAutorotateToInterfaceOrientation method in each view controller to allow all orientations when the app was running.
I then changed my code to support the new orientation methods for iOS6 but when I ran the app on the iPad, it was not rotating at all. I then changed the info.plist file to support all orientations for the iPad. This solved the problem with the iPad not autorotating but now I can't figure out how to force the app to initially open in landscape mode. Any ideas?
Thanks in advance for any help you can give!
I ran into something similar recently, although my problem was that I wanted to force portrait in an iPhone app, but allow movies to play in landscape. Here's what I did:
Add a property to my app delegate to store the orientations I want to allow:
#property (nonatomic, assign) NSUInteger supportedOrientations;
Then in the app delegate, implemented the new iOS 6 method:
- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window {
return supportedOrientations;
}
On app launch I forced portrait:
self.supportedOrientations = UIInterfaceOrientationMaskPortrait;
And then once the app had finished launching and I'd shown my root view controller, I set it to support both orientations:
self.supportedOrientations = UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskLandscape;
I had to add the additional step of subclassing my UINavigationController to prevent rotation after that, because I wanted to allow landscape video but not rotation:
- (BOOL)shouldAutorotate {
return FALSE;
}
But that's just for info. You won't need that

Supporting different orientations in iOS 6.0 for different view controllers

I am having custom split view controller in my App with a master controller and a detailed controller.
- (id)initWithMasterController:(UIViewController*)aMasterController
detailedController:(UIViewController*)aDetailedController;
The controllers provided for the master controller and details controller are UINavigationController.
As part of my app, there are two possible cases for orientation handling:
When six combination of controllers are used in master and details controller, all the orientations are supported for the app.
When there is a StudentDetailsViewController at the details controller alone, only two possible orientations can be supported. (Landscape)
When ever the device's orientation is changed, the below things happen in versions below iOS 6.0
The -shouldAutorotateToInterfaceOrientation: method gets called. The implementation of that method is below: At run time, I forward the request to master controller and details controller with the same call.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
BOOL res = [masterController shouldAutorotateToInterfaceOrientation:interfaceOrientation]
&& [detailedController shouldAutorotateToInterfaceOrientation:interfaceOrientation];
return res;
}
The masterController's -shouldAutorotateToInterfaceOrientation will return TRUE. The implementation of the method in StudentViewController is below.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (IS_IPAD) ? UIInterfaceOrientationIsLandscape(interfaceOrientation)
: UIInterfaceOrientationIsPortrait(interfaceOrientation);
}
The ability to get information on the new orientation to be changed helps me to decide if rotation should be enabled or not.
With iOS 6.0:
When ever the device's orientation is changed, the below things happen in versions of iOS 6.0
The method -shouldAutorotate of the split view controller gets called. Its implementation is below
- (BOOL)shouldAutorotate {
BOOL res = [masterController shouldAutorotate]
&& [detailedController shouldAutorotate];
return res;
}
The detailedController's shouldAutorotate calls the navigationController. The implementation of autorotate feature in StudentsController:
- (BOOL)shouldAutorotate {
return YES;
}
- (NSUInteger)supportedInterfaceOrientations {
return (UIInterfaceOrientationMaskLandscapeLeft
| UIInterfaceOrientationMaskLandscapeRight);
}
But with iOS 6.0, I am unable to control the orientation. Even though the supportedInterfaceOrientations method gets called, when the shouldAutorotate method of the StudentsDetailsController gets called, from the detailsController's shouldAutorotatemethod, the shouldAutorotateMethod does not obey the options mentioned in the supportedInterfaceOrientations method.
UPDATE:
I read the docs and the below notes are provided in the document.
sometimes you may want to dynamically disable automatic rotation. For
example, you might do this when you want to suppress rotation
completely for a short period of time. You must temporarily disable
orientation changes you want to manually control the position of the
status bar (such as when you call the
setStatusBarOrientation:animated: method).
If you want to temporarily disable automatic rotation, avoid
manipulating the orientation masks to do this. Instead, override the
shouldAutorotate method on the topmost view controller. This method is
called before performing any autorotation. If it returns NO, then the
rotation is suppressed.
Is it possible to temporarily disable automatic rotation based on the current orientation?
I believe this is some type of issue in iOS where the rootViewController does not consult the childViewController for their preferred orientation. However, you should try something like the following :
if (self.interfaceOrientation != UIInterfaceOrientationPortrait)
{
[[UIDevice currentDevice] performSelector:NSSelectorFromString(#"setOrientation:") withObject:(id)UIInterfaceOrientationPortrait];
}
to change the orientation back to portrait for a given view.
In your application delegate class define the following method, this method gets called before any other rotation methods in application.
Create a flag(isRotationEnabled) which will decide orientation of your app.
- (NSUInteger) application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window {
return self.isRotationEnabled ?
UIInterfaceOrientationMaskAll :
UIInterfaceOrientationMaskPortrait;
}
Change this flag based on different conditions in you app using the following code
MyAppDelegate *appDelegate = (MyAppDelegate *)[[UIApplication sharedApplication] delegate];
appDelegate.isRotationEnabled = NO;

Set different supportedInterfaceOrientations for different UIViewControllers

So, as by the question, I use the following code to set userinterfaceOrientation for the viewcontroller.
- (BOOL) shouldAutorotate
{
return YES;
}
-(NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskPortrait;
}
I tried setting UIInterfaceOrientationMaskAll for another viewController.
Prior, I checked all the items for Supported interface orientations in the info-plist. Both the viewControllers returned true(as set in the info-plist) for all the orientations and didn't obey the above code. IT worked even in the absence of the above code. Is there anyway to restrict certain Supported interface orientations for particular classes? I made it working for the pre - iOS6 versions by following this link.
I was able to accomplish this by placing my supported orientation logic in a custom UINavigationController. I then segue to the relevant view controller.
#implementation PortraitNavigationController
- (NSUInteger)supportedInterfaceOrientations{
return UIInterfaceOrientationMaskPortrait;
}
...
I'd say implement your shouldAutorotateToInterfaceOrientation: on every view controller to make it return only YES for those orientations you wish to support.

ipad rotation in iphone project

I've created a project that is targeted just for iPhone (for now) and I chose supported orientation only Portrait. On iPhone it works great, but when I launch my project on iPad my application is rotating! Actually I tried to make it a universal app and in iPad deployment info I chose supported orientation only Portrait, my app still rotated on iPad. How may I fix that?
To only support Portrait mode in your view, implement the following in each of your views:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
Also, you need to make sure that have set the Supported Device Orientations for each device as well:
You can manually set the rotation values in the view controllers by implementing this function:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return (interfaceOrientation == UIInterfaceOrientationPortrait;
}