xcode shouldAutorotateToInterfaceOrientation 不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2868132/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 03:24:38  来源:igfitidea点击:

shouldAutorotateToInterfaceOrientation doesn't work

iphonexcodeipaduiviewcontrollerscreen-orientation

提问by Dror Sabbag

I've been writing my Universal application in portrait mode, and now after about 15 nib files, many many viewCotnrollers, I'd like to implement the shouldAutorotateToInterfaceOrientation and design some screens in Landscape mode.

我一直在以纵向模式编写我的通用应用程序,现在经过大约 15 个 nib 文件和许多 viewCotnroller,我想实现 shouldAutorotateToInterfaceOrientation 并在横向模式下设计一些屏幕。

adding :

添加:

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

to ALL of my viewControllers, does not do the work.

对我所有的 viewControllers,都不起作用。

During Debug, i see that this method is called, but it just won't work! not in the simulator, not in the device, not in Iphone, not in Ipad!

在调试期间,我看到调用了此方法,但它不起作用!不在模拟器中,不在设备中,不在 Iphone 中,不在 Ipad 中!

i've searched some answers in the forum, and saw some advises to use:

我在论坛中搜索了一些答案,并看到了一些使用建议:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
 return (interfaceOrientation == UIInterfaceOrientationPortrait ||
   interfaceOrientation == UIInterfaceOrientationLandscapeLeft || 
   interfaceOrientation == UIInterfaceOrientationLandscapeRight ||
   interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown );
} 

Didn't worked either,

也没用

adding:

添加:

 [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];

and

 [[UIDevice currentDevice] endGeneratingDeviceOrientationNotifications];

to my viewDidLoad and viewDidUnload respectively didn't worked either.

对我的 viewDidLoad 和 viewDidUnload 也分别不起作用。

I'm lost.. Any help will do!

我迷路了.. 任何帮助都行!

just one more info... all my Views are of type UIControl, as i needed the TuchUpInside to work.

还有一个信息……我所有的视图都是 UIControl 类型,因为我需要 TuchUpInside 才能工作。

Appriciate your help.

感激你的帮助。

回答by cduhn

Make sure all of your parent views have autoresizesSubviews = YES. You may need to do this in code if you haven't set springs and struts in IB for all of your views.

确保您的所有父视图都有 autoresizesSubviews = YES。如果您没有在 IB 中为所有视图设置 springs 和 struts,您可能需要在代码中执行此操作。

Quoting the Interface Builder User's Guide:

引用 Interface Builder 用户指南:

Important: In a Cocoa nib file, if you do not set any springs or struts for your view in Interface Builder but then do use the setAutoresizingMask: method to add autosizing behavior at runtime, your view may still not exhibit the correct autoresizing behavior. The reason is that Interface Builder disables autosizing of a parent view's children altogether if those children have no springs and struts set. To enable the autosizing behavior again, you must pass YES to the setAutoresizesSubviews: method of the parent view. Upon doing that, the child views should autosize correctly.

重要提示:在 Cocoa nib 文件中,如果您没有在 Interface Builder 中为您的视图设置任何弹簧或支柱,但随后使用 setAutoresizingMask: 方法在运行时添加自动调整大小行为,您的视图可能仍不会表现出正确的自动调整大小行为。原因是如果父视图的子视图没有设置弹簧和支柱,Interface Builder 将完全禁用父视图子视图的自动调整大小。要再次启用自动调整大小行为,您必须将 YES 传递给父视图的 setAutoresizesSubviews: 方法。这样做后,子视图应该正确自动调整大小。

A couple other things to be aware of:

需要注意的一些其他事项:

  1. A UINavigationController will only autorotate if its root view controller is also set to autorotate.

  2. A UITabBarController will only autorotate if all of its view controllers are set to autorotate.

  1. UINavigationController 只有在其根视图控制器也设置为自动旋转时才会自动旋转。

  2. UITabBarController 只有在所有视图控制器都设置为自动旋转时才会自动旋转。

回答by Vineesh TP

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    UIInterfaceOrientation des=self.interfaceOrientation;

    if(UI_USER_INTERFACE_IDIOM()==UIUserInterfaceIdiomPad) //iPad
    {
        if(des==UIInterfaceOrientationPortrait||des==UIInterfaceOrientationPortraitUpsideDown)//ipad-portairait
        {

        }
        else//ipad -landscape
        {

        }
    }
    else//iphone
    {
        UIInterfaceOrientation des=self.interfaceOrientation;

        if(des==UIInterfaceOrientationPortrait||des==UIInterfaceOrientationPortraitUpsideDown) //iphone portrait
        {

        }
        else //iphone -landscape
        {

        }
    }
return YES;
}

回答by Rob

Which iOS are you building for? It was deprecated in iOS 6.0. (You should override the supportedInterfaceOrientations and preferredInterfaceOrientationForPresentation methods instead.)

您正在为哪个 iOS 构建?它在 iOS 6.0 中被弃用。(您应该重写 supportedInterfaceOrientations 和 preferredInterfaceOrientationForPresentation 方法。)

Also you can call shouldAutorotate on the UIViewController class:

你也可以在 UIViewController 类上调用 shouldAutorotate :

https://developer.apple.com/library/ios/documentation/uikit/reference/UIViewController_Class/Reference/Reference.html#//apple_ref/occ/instm/UIViewController/shouldAutorotate

https://developer.apple.com/library/ios/documentation/uikit/reference/UIViewController_Class/Reference/Reference.html#//apple_ref/occ/instm/UIViewController/shouldAutorotate



Ensure you have checked the Supported Interface Orientations within the "Summery" tab of the project settings (Select the project name in the 'Project Navigator' at the very top).

确保您已在项目设置的“摘要”选项卡中检查了支持的界面方向(在最顶部的“项目导航器”中选择项目名称)。

If you have not selected the orientations you want to use here, then the simulator/iphone will not allow the screen to change orientation.

如果您没有在此处选择要使用的方向,则模拟器/iphone 将不允许屏幕更改方向。

回答by Tom Kincaid

I had this problem but it worked in iOS6 but not iOS5. Turns out I had a view in my storyboard that I hadn't made a viewcontroller class for yet.

我遇到了这个问题,但它在 iOS6 中有效,但在 iOS5 中无效。原来我在我的故事板中有一个视图,我还没有为它创建一个视图控制器类。

回答by newenglander

...and last but not least, make sure you haven't activated the setting "Portrait Orientation Locked" on your test device (of course doesn't apply to the simulator), this will disable rotating in any app no matter what shouldAutorotateToInterfaceOrientationreturns.

...最后但并非最不重要的一点,请确保您没有在测试设备上激活设置“纵向锁定”(当然不适用于模拟器),无论shouldAutorotateToInterfaceOrientation返回什么,这都会禁用任何应用程序中的旋转.