Stop UIView from roating into landscape
Asked Answered
A

3

2

I have a UIView thats inside a navigation controller, I am trying to prevent this view from going into Landscape however the method I am trying to use never fires.. code is below any help would be greatly appreciated..

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    if(interfaceOrientation == UIInterfaceOrientationLandscapeRight)
    {
        return NO;
    }
    else if(interfaceOrientation == UIInterfaceOrientationLandscapeLeft)
    {
        return NO;
    }
    return NO;
}
Anew answered 11/7, 2012 at 21:27 Comment(1)
is that method in a view or a view controller?Leatrice
I
4

You should set return NO; for the parent navigation controller or on a UIViewController, not a UIView.

Also, this works just the same with less code:

iOS 5.x

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

iOS 6

As of iOS 6, shouldAutorotateToInterfaceOrientation: is deprecated. If the view controller does not override the supportedInterfaceOrientations method, UIKit obtains the default rotations from the app delegate or the app’s Info.plist file.

You will need to use:

- (NSUInteger)supportedInterfaceOrientations NS_AVAILABLE_IOS(6_0);
Inwardly answered 11/7, 2012 at 21:33 Comment(12)
As I remarked in my response, always returning NO is considered improper and (last I checked) triggers a run-time warning.Inequity
sorry this is not working for me.. as from another answer I read that its on invoked on a view but on the viewcontroller. I have a UIView in InterfaceBuilder which is linked to the fileOwner as a view.. but I just dunno what else to look for to get this thing to stop rotating.Anew
Place this code on your parent UINavigationController, as explained, shouldAutorotateToInterfaceOrientation: is apart of the UIViewController class and will not work on a UIView.Inwardly
right.. I understand now.. I should also tell you that I was only wanting to prevent this view from rotating not every view in my NavigationController.. is there a way to achieve this?Anew
Yes, you can set a BOOL option to see if the view is visible. If it is, then disable rotation. So when you show the view, set something like myViewIsShowing = YES;. if (myViewIsShowing) /*do not rotate */;Inwardly
Okay I'm having difficulty here :( I know that you want me to place the code on my parent UINavController, however I am using a master-detail Application, the masterView is simply a UIViewController of a uiview, the NavigationController is declared in the appDelegate.. I'm kinda lost as to where I can put this code to get it to work.. i have tried everywhere and still no success..Anew
You should probably read the Apple docs on both UIViewController and UINavigationController to get a better understanding.Inwardly
Have you used the template "master-detail Application"?Anew
On many occasions. The template you use makes no difference. The classes still require the same methods.Inwardly
humor me, start your own "master-detail Application" thats standard in the apple templates when it loads go to masterViewController.m in there you will see the exact same method you have suggested above, change it from != UIInterfaceOrientationPortraitUpsideDown to what you have suggested == UIInterfaceOrientationPortrait then run it on a device and see what happens.. the damn thing is broken.. unless I am going absolutely crazy, I have tried this twice on my project which is very similar to the template just with few alterations and then on a completely fresh template.Anew
I don't understand the syntax : - (NSUInteger)supportedInterfaceOrientations NS_AVAILABLE_IOS(6_0);. Is that really what I have to type ? If it is, it's the 1st time I see that.Hydrothorax
No, NS_AVAIL just tells you that it's available only in iOS 6. It's from the Apple docs.Inwardly
A
1

Another option is modifying Deployment Info.

Screenshot

Armhole answered 10/9, 2014 at 23:29 Comment(0)
I
0

This method needs to be implemented in a UIViewController instance, not a UIView.

Also, you are in fact returning NO for all orientations, which is incorrect. You need to return YES for at least one orientation (most commonly UIInterfaceOrientationPortrait).

Inequity answered 11/7, 2012 at 21:33 Comment(1)
woopsie!!! i had it at yes at one point and in my frantic state of trying to find a solution must have changed that for some weird reason.Anew

© 2022 - 2024 — McMap. All rights reserved.