18

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

添加:

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

到我所有的 viewControllers,不做这项工作。

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

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

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

也没有用,

添加:

 [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];

 [[UIDevice currentDevice] endGeneratingDeviceOrientationNotifications];

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

我迷路了..任何帮助都可以!

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

Appriciate your help.

4

5 回答 5

44

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.

Quoting the Interface Builder User's Guide:

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.

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.

于 2010-05-20T15:32:15.530 回答
2
- (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;
}
于 2012-05-15T07:48:14.753 回答
1

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

Also you can call shouldAutorotate on the UIViewController class:

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.

于 2013-09-23T12:23:04.890 回答
0

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.

于 2012-10-17T21:15:30.807 回答
0

...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 shouldAutorotateToInterfaceOrientation returns.

于 2012-11-12T21:17:37.600 回答