4

I have an app written in an older version of Xcode. Right now I'm using Xcode 4.6.1. This is an app I inherited from another developer that was developed outside the company.

One of the problems with this app is its interface. It should default to LandscapeLeft (this is the value set in the pList file). The app ignores this directive and instead defaults to portrait mode.

Is there something I need to do to force the code to honor this value? I'm relatively new to iOS/Objective-C. The code compiles and runs, it's just the interface isn't doing what I want.

It's a little easier since this is an iPad only app.

Edit I've tried adding the following code to one of my problematic viewControllers, but it's not being honored. Any thoughts on how to force this view to landscape? (Is there a setting in the IDE - it looks like Xcode used to have an orientation attribute, but I can't find it in 4.6.1)

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Overriden to allow any orientation.
    return ((interfaceOrientation==UIInterfaceOrientationMaskLandscapeLeft) || (interfaceOrientation == UIInterfaceOrientationMaskLandscapeRight));
}

- (NSUInteger) supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskLandscapeLeft;
}

- (NSInteger) supportedInterfaceOrientationsForWindow
{
    return UIInterfaceOrientationMaskLandscape;
}

- (BOOL) shouldAutoRotate
{
    return YES;
}
Paul Renton
  • 2,652
  • 6
  • 25
  • 38
Tim
  • 4,051
  • 10
  • 36
  • 60
  • What happens if you make `-shouldAutoRotate` return false? – Richard J. Ross III May 02 '13 at 18:46
  • @Richard - nothing. That was one of the first things I tried when it didn't work. – Tim May 02 '13 at 18:52
  • Can you be more specific on which view controllers are giving you an issue? (e.g. what do they subclass, any interesting notes, etc.) – Richard J. Ross III May 02 '13 at 18:59
  • In case you are talking about initial orientation of the app, then change the orientation order in plist. Like UIInterfaceorientationLandscapeLeft, landscape right, portrait. – Bharath May 04 '13 at 15:01
  • What version of iOS are you building for? And what OS are you testing on the devices? – Fogmeister May 06 '13 at 20:05
  • We're building on iOS 5 and 6. (We have some first-generation iPads we need to support until their batteries start dying) – Tim May 06 '13 at 20:07
  • possible duplicate of [Understanding iOS 6 Interface orientation change](http://stackoverflow.com/questions/12778636/understanding-ios-6-interface-orientation-change) – matt May 17 '13 at 02:14

12 Answers12

7

I hope this will help you. Add one of this(which orientation you want) method in AppDelegate.m class

For force fully run application in landscape mode:

- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
{
    return UIInterfaceOrientationMaskLandscape;
}

For force fully run application in Portrait mode:

- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
{
    return UIInterfaceOrientationMaskPortrait;
}
Ashvin
  • 8,227
  • 3
  • 36
  • 53
  • Thanks, John! This actually works with new iPads and old ones as well. (I need to support iPad 1s through the latest version) – Tim Jun 25 '13 at 18:19
3

Check whether view controllers in the app force view orientation. Check section 'Handling View Rotations' and tasks related to rotation in Apple's documentation:

http://developer.apple.com/library/ios/documentation/UIKit/Reference/UIViewController_Class/Reference/Reference.html

gberginc
  • 447
  • 2
  • 4
  • I've looked at the documentation and implemented the recommended methods - still no luck. Some of my viewControllers default to Portrait and others default to Landscape. The correct format *should* be Landscape. This is frustrating to say the least... – Tim Apr 30 '13 at 16:40
1

It seems like you have all the rotation code properly implemented in your view controllers so maybe this is your problem. A while back i was in your shoes where i set up my code just how apple specified it to be in order to load my app in landscape mode but for some reason it just wasnt loading the app correctly. So after a bunch of forum posts and many many forum replies it turned out in my view controller i wasnt setting the view controllers view in loadView. So make sure in your load view method of the view controller the view you want the root view to be is set as is the view controllers self.view. Tell me how it goes

Esko918
  • 1,397
  • 1
  • 12
  • 25
1

Check post Launching application in landscape orientation for IPad
Quoting jhhl from there:

setting the initial interface orientation in your info.plist is being ignored if you have Supported interface orientations set up with another orientation in the first slot! Put your initial orientation there as well - and the simulator will launch correctly, as will the app

Edit: one more thing - the problem may be in completely different place. Are you sure you have proper view controller hierarchy? If a controller has no been added the right way to the window or another controller the rotation events and logic will not work. In fact I just checked an app I wrote a couple of months ago which had the same issue - iPad, must start in landscape. Didn't take anything fancy, but setting the orientations in the Info.plist, attaching the rootViewController and setting the proper return values in the rotation methods.

Community
  • 1
  • 1
Dimitar K
  • 794
  • 5
  • 9
  • This! My iPad app would start in the wrong orientation for a brief moment. I had to change the order of supported orientations so that Portrait was the 1st one (since I wanted the app to start in portrait) – Mihai Fratu Jul 14 '22 at 09:47
0

Click on your Project file in Xcode. Then click on 'Targets'. Choose you orientation (Scroll down!) Make sure in IB that the View (Under inspector) is set to portrait.

Keaton
  • 127
  • 14
  • Tried that, too. Unfortunately there are certain viewControllers that aren't honoring my app settings. (It's set to landscape, but they go to portrait mode) – Tim Apr 30 '13 at 20:18
0

Launching in Landscape Mode

In iOS 5 you need to implement the following method in everyone of your view controllers.

// Called only in IO 5 and earlier.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationLandscapeRight ||
            interfaceOrientation == UIInterfaceOrientationLandscapeLeft );
}

And set UIInterfaceOrientation "Initial interface orientation " to UIInterfaceOrientationLandscapeRight in your Info.plist

And lay out your views in landscape mode.

Per Apple's developer docs

GayleDDS
  • 4,443
  • 22
  • 23
0

Prior to iOS 6 i.e in iOS 5 and earlier, an app and a view controller’s rotation is controlled by the individual viewcontrollers while in iOS 6 and later, the view controllers responsible for rotation are the container Viewcontrollers such as UINavigationController & UITabBarController . What are you using as the rootviewcontroller in your project??

Autorotation is clearly explained here in this post- Autorotation in iOS

Xcoder
  • 1,762
  • 13
  • 17
0

Try this, we had the same issue.

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    BOOL returningValue=NO;

    if (interfaceOrientation==UIInterfaceOrientationIsPortrait(interfaceOrientation))
    {
        returningValue=NO;
    }
    else if(UIInterfaceOrientationIsLandscape(interfaceOrientation))
    {
        returningValue=YES;
    }

    return returningValue;
}
Sharon Nathaniel
  • 1,467
  • 20
  • 28
  • You realize this is a very verbose way of writing `return (UIInterfaceOrientationIsLandscape(interfaceOrientation)`, right? – pille May 07 '13 at 08:44
  • Yes! But this is the only code among many that we tried and this worked perfect for us on both iOS 5 and iOS 6. – Sharon Nathaniel May 07 '13 at 08:47
0

In older Xcode versions, all you have to do is go the .xib file, select the box with the arrows that says Portrait, and change it to Landscape, and then in the .m file, do:

 - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Overriden to allow any orientation.
return interfaceOrientation==UIInterfaceOrientationLandscapeLeft || interfaceOrientation == UIInterfaceOrientationLandscapeRight;
 }
user2277872
  • 2,963
  • 1
  • 21
  • 22
0

Just open your plist and edit the "Supported interface orientations" and drag the "Landscape (left home button)" to the top over the "Portrait (bottom home button)"

by default the top value is loaded first if you are supporting pre-iOS6 and running this in post-iOS6 SDK.

K..
  • 77
  • 9
0

Are you using iOS 6.0 or higher? Then try implementing the preferredInterfaceOrientationForPresentation method also.

- (UIInterfaceOrientation) preferredInterfaceOrientationForPresentation
{
    return UIInterfaceOrientationLandscapeLeft;
}

In case your root view is a UINavigationController you might want to subclass that and implement the new orientation methods in it.

When I want to support different default orientation in different UIViewController pushed to the same navigation controller I have a custom UINavigationControler kind of like this:

@implementation MyNavigationController

- (BOOL)shouldAutorotate;
{
    return YES;
}

- (NSUInteger) supportedInterfaceOrientations
{
    if ([[self topViewController] respondsToSelector:@selector(supportedInterfaceOrientations)])
        return [[self topViewController] supportedInterfaceOrientations];
    else
        return [super supportedInterfaceOrientations];
}

- (UIInterfaceOrientation) preferredInterfaceOrientationForPresentation
{
    if ([[self topViewController] respondsToSelector:@selector(preferredInterfaceOrientationForPresentation)])
        return [[self topViewController] preferredInterfaceOrientationForPresentation];
    else
        return [super preferredInterfaceOrientationForPresentation];
}

@end
osanoj
  • 1,035
  • 10
  • 9
0

The rootViewController-property was added to the UIWindow-class in iOS 4. Usually this value is set in the UIApplicationDelegate (in your case probably AppDelegate.m). If the rootViewController-property is not correct rotation-events sometimes don't get passed properly through the view-hierachy.

Try self.window.rootViewController = myRootViewController; if it's not there already.

RhodanV5500
  • 1,087
  • 12
  • 16