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;
}
HurkNburkS
  • 5,492
  • 19
  • 100
  • 183

3 Answers3

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);
WrightsCS
  • 50,551
  • 22
  • 134
  • 186
  • As I remarked in my response, always returning `NO` is considered improper and (last I checked) triggers a run-time warning. – Conrad Shultz Jul 11 '12 at 21:34
  • 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. – HurkNburkS Jul 11 '12 at 21:40
  • Place this code on your parent `UINavigationController`, as explained, `shouldAutorotateToInterfaceOrientation:` is apart of the `UIViewController` class and will not work on a `UIView`. – WrightsCS Jul 11 '12 at 21:43
  • 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? – HurkNburkS Jul 11 '12 at 21:45
  • 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 */;` – WrightsCS Jul 11 '12 at 21:51
  • 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.. – HurkNburkS Jul 11 '12 at 22:03
  • You should probably read the Apple docs on both `UIViewController` and `UINavigationController` to get a better understanding. – WrightsCS Jul 11 '12 at 22:04
  • Have you used the template "master-detail Application"? – HurkNburkS Jul 11 '12 at 22:07
  • 1
    On many occasions. The template you use makes no difference. The classes still require the same methods. – WrightsCS Jul 11 '12 at 22:11
  • 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. – HurkNburkS Jul 11 '12 at 22:24
  • 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. – Lucien Jun 04 '13 at 10:58
  • No, NS_AVAIL just tells you that it's available only in iOS 6. It's from the Apple docs. – WrightsCS Jun 04 '13 at 15:36
1

Another option is modifying Deployment Info.

Screenshot

Dehli
  • 5,950
  • 5
  • 29
  • 44
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).

Conrad Shultz
  • 8,748
  • 2
  • 31
  • 33
  • 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. – HurkNburkS Jul 11 '12 at 21:37