force landscape ios 7
Asked Answered
P

3

10

I've tried to following methods to force landscape on one my views:

- (NSUInteger)supportedInterfaceOrientations {
    return UIInterfaceOrientationMaskLandscape;
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
    return UIInterfaceOrientationLandscapeLeft;
}

- (BOOL)shouldAutorotate{
    return YES;
}

Neither did work. Note that I'm testing on the simulator and on an iPad.

Thanks

Proconsulate answered 30/9, 2013 at 13:11 Comment(0)
P
22

Here is how I forced one of my views to be Landscape using NavigationViewController:

  1. Implemented this answer: https://mcmap.net/q/539017/-how-to-make-app-fully-working-correctly-for-autorotation-in-ios-6

  2. Imported message in the View controller: objc/message.h

  3. Added this line of code in the viewDidLoad method:

objc_msgSend([UIDevice currentDevice], @selector(setOrientation:), UIInterfaceOrientationLandscapeLeft);

Hope it helps someone.

Proconsulate answered 1/10, 2013 at 11:11 Comment(6)
Will this logic be approved for Apple?Benzoin
It looks like all I needed was steps 2&3 (1 seems to be optional)Cento
with step 1 you prevent the user from going portrait againProconsulate
I have it running in two appsProconsulate
this seems to work in the iOS simulator but not on the actual device?Mosher
This is definitely wrong. If you're trying to force a particular interface orientation you want to call [UIApplication sharedApplication].statusBarOrientation = UIInterfaceOrientationPortrait; Apple has provided two different enums that are similar, but very different: UIDeviceOrientation and UIInterfaceOrientation. The device property is read-only, because you can't force the physical device to change orientation! The statusBarOrientation is writable, and will do what you were trying to do with setValue:forKey:Mylander
M
7

The accepted answer doesn't seem to work on iOS 7.1.2. (It works on the simulator but does not work while running on a device.)

This seems to work though:

[[UIDevice currentDevice] setValue:[NSNumber numberWithInteger:UIInterfaceOrientationPortrait] forKey:@"orientation"];

[[UIDevice currentDevice] setValue:[NSNumber numberWithInteger:UIInterfaceOrientationLandscapeLeft] forKey:@"orientation"];
Mosher answered 1/8, 2014 at 13:24 Comment(1)
my solution is working in an iPhone 4 with iOS 7.1.2. What's your device?Proconsulate
G
1
int shouldShowInLandscape = 0;

-(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {


    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(forceToLandscape:)
                                                 name:@"yourNameNotification"
                                               object:nil];

}

-(NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
{

    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
    {
              return UIInterfaceOrientationMaskLandscapeLeft;
    } else {
            if(shouldShowInLandscape)
            {
                return UIInterfaceOrientationMaskLandscape;
            }
            return UIInterfaceOrientationMaskPortrait;
    }
}

-(void) forceToLandscape:(NSNotification*) theNot
{
    shouldShowInLandscape = [[theNot object] intValue];

}

// from the UIViewController

- (void)viewDidLoad
{
    [super viewDidLoad];

    [[NSNotificationCenter defaultCenter]
     postNotificationName:@"yourNameNotification"
     object:[NSString stringWithFormat:@"1"]];
}

-(void) viewDidDisappear:(BOOL)animated
{

        [[NSNotificationCenter defaultCenter]
     postNotificationName:@"yourNameNotification"
     object:[NSString stringWithFormat:@"0"]]; 

}

// remove you notificationCenter

Geter answered 9/8, 2014 at 0:5 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.