Stop UIView from roating into landscape

2019-07-22 17:37发布

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;
}

3条回答
SAY GOODBYE
2楼-- · 2019-07-22 17:44

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).

查看更多
倾城 Initia
3楼-- · 2019-07-22 17:47

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);
查看更多
Juvenile、少年°
4楼-- · 2019-07-22 18:09

Another option is modifying Deployment Info.

Screenshot

查看更多
登录 后发表回答