iOS 6.0 中的界面方向
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12404556/
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
Interface orientation in iOS 6.0
提问by
How to use the following methods to support interface orientation in iOS 6.0:
如何在iOS 6.0中使用以下方法支持界面方向:
shouldAutorotate
supportedInterfaceOrientations
preferredInterfaceOrientationForPresentation
As "shouldAutorotateToInterfaceOrientation" is deprecated in iOS 6.0.
由于“shouldAutorotateToInterfaceOrientation”在iOS 6.0 中已弃用。
Please provide code snippets to support your answers.
请提供代码片段以支持您的答案。
Thanks.
谢谢。
回答by uerceg
Deprecated method in iOS 5:
iOS 5 中已弃用的方法:
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationLandscapeRight);
}
Replacement in iOS 6 and equivalent of this deprecated iOS 5 method above:
iOS 6 中的替换和上面这个已弃用的 iOS 5 方法的等效项:
- (BOOL) shouldAutorotate
{
return YES;
}
-(NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskLandscapeRight;
}
Hope this helps.
希望这可以帮助。
[edit #1: Added my UIViewController which successfully starts in Portrait mode in XCode 4.5 on iPhone 6.0 Simulator]
[编辑 #1:添加了我的 UIViewController,它在 iPhone 6.0 模拟器上的 XCode 4.5 中以纵向模式成功启动]
#import "FirstViewController.h"
@interface FirstViewController ()
@end
@implementation FirstViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self)
{
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}
-(BOOL)shouldAutorotate
{
return NO;
}
-(NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskAll;
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
return UIInterfaceOrientationMaskPortrait;
}
[#edit 2: Sample code from landscape only application which supports iOS 5 and iOS 6]
[#edit 2:来自仅支持 iOS 5 和 iOS 6 的横向应用程序的示例代码]
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return (interfaceOrientation == UIInterfaceOrientationLandscapeRight) || (interfaceOrientation == UIInterfaceOrientationLandscapeLeft);
}
- (BOOL)shouldAutorotate {
return YES;
}
- (NSUInteger)supportedInterfaceOrientations {
return UIInterfaceOrientationMaskLandscape;
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
return UIInterfaceOrientationLandscapeLeft;
}
回答by James Laurenstin
By the way, your settings on your Xcode project settings now take precedence. Make sure that you set the "Supported interface orientations" array properly in your project's settings. That was the issue for me. Removed the undesired ones and my app worked like it did when I compiled with Xcode 4.4.1
顺便说一下,您对 Xcode 项目设置的设置现在优先。确保在项目设置中正确设置了“支持的界面方向”数组。这就是我的问题。删除了不需要的那些,我的应用程序就像我用 Xcode 4.4.1 编译时一样工作
回答by Nicolas Miari
My app has an instance of a custom UINavigationController subclass, that presents several view controllers, all in portrait only, except when playing a video, in which case I want to additionally allow both landscape orientations.
我的应用程序有一个自定义 UINavigationController 子类的实例,它提供了几个视图控制器,全部都是纵向的,除了播放视频时,在这种情况下,我想另外允许两个横向。
Based on @uerceg 's answer, this is my code.
根据@uerceg 的回答,这是我的代码。
First, I enabled Portrait, Landscape Left and Landscape right in Xcode -> Target -> Summary.
首先,我在 Xcode -> Target -> Summary 中启用了 Portrait、Landscape Left 和 Landscape right。
In the UINavigationController subclass's implementation, I #import
'ed <MediaPlayer/MediaPlayer.h>
.
在 UINavigationController 子类的实现中,我#import
ed <MediaPlayer/MediaPlayer.h>
.
Then I implemented these methods:
然后我实现了这些方法:
// Autorotation (iOS <= 5.x)
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
if ([self modalViewController] && [[self modalViewController] isKindOfClass:[MPMoviePlayerController class]]) {
// Playing Video: Anything but 'Portrait (Upside down)' is OK
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}
else{
// NOT Playing Video: Only 'Portrait' is OK
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
}
// Autorotation (iOS >= 6.0)
- (BOOL) shouldAutorotate
{
return YES;
}
-(NSUInteger)supportedInterfaceOrientations
{
NSUInteger orientations = UIInterfaceOrientationMaskPortrait;
if ([self modalViewController] && [[self modalViewController] isKindOfClass:[MPMoviePlayerController class]]) {
// Playing Video, additionally allow both landscape orientations:
orientations |= UIInterfaceOrientationMaskLandscapeLeft;
orientations |= UIInterfaceOrientationMaskLandscapeRight;
}
return orientations;
}
回答by user593733
NicolasMiari's code worked for me. A little different spin I had a UITabBarController that presented UINavigationControllers and I was using StoryBoards. The UITabBarController's subclass's implementation is exactly the same and be patient with the Class selection for the Tab Bar Controller in Story Boards. It isn't immediately available even after building.
NicolasMiari 的代码对我有用。有点不同的旋转我有一个呈现 UINavigationControllers 的 UITabBarController,我使用的是 StoryBoards。UITabBarController 的子类的实现完全相同,请耐心等待 Story Boards 中 Tab Bar Controller 的 Class 选择。即使在构建之后,它也不会立即可用。
回答by Deji
https://devforums.apple.com/thread/165384?tstart=0
https://devforums.apple.com/thread/165384?tstart=0
https://devforums.apple.com/thread/166544?tstart=0
https://devforums.apple.com/thread/166544?tstart=0
There are a number of examples and suggestions in the above threads relating to supporting interface orientation changes on iOS6, the two threads related to issues with game centre views but should be enough to get you started.
上述线程中有许多与支持 iOS6 上的界面方向更改相关的示例和建议,这两个线程与游戏中心视图问题相关,但应该足以让您入门。
You should also check the iOS6 release notes under UIKit, unfortunately I can't give you a direct link since I'm new.
您还应该查看 UIKit 下的 iOS6 发行说明,不幸的是,由于我是新手,我无法给您直接链接。
Avoiding posting code here due to NDA
由于保密协议,避免在此处发布代码
Hope that helps
希望有帮助