ios 从 iPhone 6 Plus 主屏幕横向启动到纵向会导致方向错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26003086/
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
Launching into portrait-orientation from an iPhone 6 Plus home screen in landscape orientation results in wrong orientation
提问by jaredsinclair
The actual title for this question is longer than I can possibly fit:
这个问题的实际标题比我可能适合的要长:
Launching an app whose root view controller only supports portrait-orientation but which otherwise supports landscape orientations on an iPhone 6 Plus while the home screen is in a landscape orientation results in a limbo state where the app's window is in a landscape orientation but the device is in a portrait orientation.
在 iPhone 6 Plus 上启动一个根视图控制器仅支持纵向但在 iPhone 6 Plus 上支持横向的应用程序,同时主屏幕处于横向会导致应用程序的窗口处于横向但设备处于不确定状态在纵向。
In short, it looks like this:
简而言之,它看起来像这样:
When it is supposed to look like this:
当它看起来像这样时:
Steps to Reproduce:
重现步骤:
iPhone 6 Plus running iOS 8.0.
An app whose plist supports all-but-portrait-upside-down orientations.
The root view controller of the app is a UITabBarController.
Everything, the tab bar controller and all its descendent child view controllers return
UIInterfaceOrientationMaskPortrait
fromsupportedInterfaceOrientations
.Start at iOS home screen.
Rotate to landscape orientation (requires iPhone 6 Plus).
Cold-launch the app.
Result: broken interface orientations.
运行 iOS 8.0 的 iPhone 6 Plus。
一个应用程序,其 plist 支持所有但纵向颠倒的方向。
应用程序的根视图控制器是一个 UITabBarController。
一切,标签栏控制器及其所有后代子视图控制器都
UIInterfaceOrientationMaskPortrait
从supportedInterfaceOrientations
.从 iOS 主屏幕开始。
旋转到横向(需要 iPhone 6 Plus)。
冷启动应用程序。
结果:界面方向损坏。
I can't think of any other way to enforce a portrait orientation exceptto disable landscape altogether, which I can't do: our web browser modal view controllers need landscape.
除了完全禁用横向之外,我想不出任何其他方式来强制执行纵向,而我不能这样做:我们的 Web 浏览器模式视图控制器需要横向。
I even tried subclassing UITabBarController and overriding supportedInterfaceOrientations to return the portrait-only mask, but this (even with all the other steps above) did not fix the issue.
我什至尝试子类化 UITabBarController 并覆盖 supportedInterfaceOrientations 以返回仅限纵向的掩码,但这(即使执行了上述所有其他步骤)并没有解决问题。
Here's a link to a sample project showing the bug.
采纳答案by jaredsinclair
This appears to be a bug in iOS 8 when using a UITabBarController as a root view controller. A workaround is to use a mostly vanilla UIViewController as the root view controller. This vanilla view controller will serve as the parent view controller of your tab bar controller:
当使用 UITabBarController 作为根视图控制器时,这似乎是 iOS 8 中的一个错误。一种解决方法是使用一个主要是普通的 UIViewController 作为根视图控制器。这个香草视图控制器将作为你的标签栏控制器的父视图控制器:
///------------------------
/// Portrait-Only Container
///------------------------
@interface PortraitOnlyContainerViewController : UIViewController
@end
@implementation PortraitOnlyContainerViewController
- (NSUInteger)supportedInterfaceOrientations {
return UIInterfaceOrientationMaskPortrait;
}
@end
// Elsewhere, in app did finish launching ...
PortraitOnlyContainerViewController *container = nil;
container = [[PortraitOnlyContainerViewController alloc]
initWithNibName:nil
bundle:nil];
[container addChildViewController:self.tabBarController];
self.tabBarController.view.frame = container.view.bounds;
[container.view addSubview:self.tabBarController.view];
[self.tabBarController didMoveToParentViewController:container];
[self.window setRootViewController:container];
回答by Stefan Church
I had the same issue when launching our app in landscape on an iPhone 6 Plus.
在 iPhone 6 Plus 上横向启动我们的应用程序时,我遇到了同样的问题。
Our fix was to remove landscape supported interface orientations from the plist via project settings:
我们的修复是通过项目设置从 plist 中删除支持横向的界面方向:
and implement application:supportedInterfaceOrientationsForWindow: in the app delegate:
并在应用程序委托中实现 application:supportedInterfaceOrientationsForWindow::
- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window {
return UIInterfaceOrientationMaskAllButUpsideDown;
}
Apparently the information in your plist is to specify what orientations your app is allowed to launch to.
显然,您的 plist 中的信息是指定允许您的应用程序启动的方向。
回答by neilvillareal
Setting the statusBarOrientation
of the UIApplication
seems to work for me. I placed it in the application:didFinishLaunchingWithOptions:
method in the app delegate.
设置statusBarOrientation
的UIApplication
,似乎为我工作。我把它放在application:didFinishLaunchingWithOptions:
应用程序委托的方法中。
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
application.statusBarOrientation = UIInterfaceOrientationPortrait;
// the rest of the method
}
回答by Eurico Dtheitroadado
I had a very similar problem. I wanted to force portrait mode everywhere except for playing back videos.
我有一个非常相似的问题。除了播放视频外,我想在任何地方强制使用纵向模式。
What I did was:
我所做的是:
1) to force the app orientation to be in portrait in the AppDelegate:
1) 在 AppDelegate 中强制应用程序方向为纵向:
-(NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
{
if ([window.rootViewController.presentedViewController isKindOfClass:[MPMoviePlayerViewController class]])
{
return UIInterfaceOrientationMaskAll;
}
return UIInterfaceOrientationMaskPortrait;
}
2) launching an empty modal view controller fixed the problem in my case. I launch it in the viewDidLoad of the first view controller that is on the root of my NavigationViewController (the first view controller visible after the application launches):
2)启动一个空的模态视图控制器解决了我的问题。我在位于 NavigationViewController 根目录的第一个视图控制器的 viewDidLoad 中启动它(应用程序启动后可见的第一个视图控制器):
- (void)showAndHideNamelessViewControllerToFixOrientation {
UIViewController* viewController = [[UIViewController alloc] init];
[self presentViewController:viewController animated:NO completion:nil];
[viewController dismissViewControllerAnimated:NO completion:nil];
}
回答by Anthony
No luck for me the workaround by Jared using a generic container view controller. I've already subclassed tab bar controller with supportedInterfaceOrientations with no luck as well. Regardless of orientation of the 6+ after launch the tab bar's window is reporting frame = (0 0; 736 414)
Jared 使用通用容器视图控制器的解决方法对我来说不走运。我已经使用 supportedInterfaceOrientations 子类化了标签栏控制器,但也没有运气。无论启动后 6+ 的方向如何,标签栏的窗口都在报告frame = (0 0; 736 414)
So far the only workaround I've found is to force the window frame after makeKeyAndVisible
到目前为止,我发现的唯一解决方法是在 makeKeyAndVisible 之后强制窗口框架
[self.window makeKeyAndVisible];
self.window.frame = CGRectMake(0, 0, MIN(CGRectGetWidth(self.window.frame), CGRectGetHeight(self.window.frame)), MAX(CGRectGetWidth(self.window.frame), CGRectGetHeight(self.window.frame)));
[self.window makeKeyAndVisible];
self.window.frame = CGRectMake(0, 0, MIN(CGRectGetWidth(self.window.frame), CGRectGetHeight(self.window.frame)), MAX(CGRectGetWidth(self.window.frame), CGRectGetHeight(self.window.frame)));
回答by fumiaki
Please try the following code. Probably this problem caused by size of keywindow on landscape launch.
请尝试以下代码。这个问题可能是由横向启动时的 keywindow 大小引起的。
// in application:didFinishLaunchingWithOptions: ...
self.window.rootViewController = self.viewController;
[self.window makeKeyAndVisible];
[self.window setFrame:[[UIScreen mainScreen] bounds]]; //<- ADD!!
回答by Hyman Humphries
I only want my app to open in landscape mode (and not exhibit the problem you describe above on the iPhone 6 Plus), so I set Landscape (left home button)
and Landscape (right home button)
as the only orientations allowed in my app's PLIST file. This fixes the orientation problem when my app opens. However, I need my app to support portrait mode for one view only since I display a UIImagePickerController
in my app, which Apple requires to be shown in portrait mode on iPhone.
我只希望我的应用程序以横向模式打开(并且不会出现您在 iPhone 6 Plus 上描述的上述问题),因此我将Landscape (left home button)
和设置Landscape (right home button)
为我的应用程序的 PLIST 文件中允许的唯一方向。这解决了我的应用程序打开时的方向问题。但是,我需要我的应用程序仅支持一个视图的纵向模式,因为我UIImagePickerController
在我的应用程序中显示了一个,Apple 要求在 iPhone 上以纵向模式显示。
I was able to support portrait for that one view only, while keeping my app opening in landscape mode, by including the following code in AppDelegate
:
通过在 中包含以下代码,我能够仅支持该视图的纵向,同时保持我的应用程序以横向模式打开AppDelegate
:
-(NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window {
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) {
return UIInterfaceOrientationMaskAllButUpsideDown;
} else {
return UIInterfaceOrientationMaskAll;
}
}
回答by Gürhan KODALAK
I got same bug on my app, I figured it out with this solution
我的应用程序遇到了同样的错误,我用这个解决方案解决了这个问题
Firstly it didn't work but after some dig I have to do it on initial controller after splash screen.
首先它不起作用,但经过一些挖掘后,我必须在启动画面后在初始控制器上进行。
Answer is OjbC language let me update it to Swift
答案是 OjbC 语言让我将其更新为 Swift
override var shouldAutorotate: Bool {
return true
}
override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
return .portrait
}
Don't forget that should on the initial view controller.
不要忘记应该在初始视图控制器上。
回答by de-bits
I had a similar issue is with my app runs both in landscape and portrait with a UITabBarController as root view controller.
我有一个类似的问题是我的应用程序以 UITabBarController 作为根视图控制器以横向和纵向运行。
Whenever the app was launched when in Landscape mode, the view was incorrect.
每当应用程序在横向模式下启动时,视图都是不正确的。
All I had to do: - remove rootview controller assignment in the XIB. - Manually add it in once the app is launched:
我所要做的: - 删除 XIB 中的 rootview 控制器分配。- 应用启动后手动添加:
(void)applicationDidFinishLaunching:(UIApplication *)application { application.statusBarHidden = YES;
[self.window setRootViewController:self.tabBarController];
(void)applicationDidFinishLaunching:(UIApplication *)application { application.statusBarHidden = YES;
[self.window setRootViewController:self.tabBarController];
That fixed the problem.
这解决了问题。