问题描述
我有一个空的应用程序,没有涉及故事板或xib。我希望有一个隐藏的状态栏,只支持横向。同样,我不想仅在代码中进行这些更改,也不要触摸Info.plist。
I have an empty application and there is no storyboard or xib involved. I want to have a hidden status bar and support only landscape orientation. Again, I wan't to make those changes only within code and don't touch the Info.plist.
我创建了一个带有控制器的UIWindow,该控制器表示唯一支持的方向是横向。在这种情况下,我的UIWindow是在纵向模式的维度中创建的,不会旋转。预期的结果将是一个完全是青色的屏幕。
I create a UIWindow with a controller that says the only supported orientation is landscape. In that case my UIWindow is created in the dimension of portrait mode and doesn't rotate. The expected result would be a screen that is completely cyan.
这是我的代表:
#import "AppDelegate.h"
#import "AppViewController.h"
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
self.window.backgroundColor = [UIColor cyanColor];
self.window.rootViewController = [[AppViewController alloc] init];
[self.window makeKeyAndVisible];
return YES;
}
@end
这是我的控制器:
#import "AppViewController.h"
@implementation AppViewController
- (BOOL)shouldAutorotate {
return YES;
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
return UIInterfaceOrientationLandscapeLeft;
}
- (BOOL)prefersStatusBarHidden {
return YES;
}
- (NSUInteger)supportedInterfaceOrientations {
return UIInterfaceOrientationMaskLandscape;
}
@end
我尝试过的远
如果我在调用makeKeyAndVisible后设置了rootViewController,那么一切似乎都起作用。
What I've tried so far
If I set the rootViewController after calling makeKeyAndVisible everything seems to work at first.
self.window.backgroundColor = [UIColor cyanColor];
[self.window makeKeyAndVisible];
self.window.rootViewController = [[AppViewController alloc] init];
还有一些问题。首先,我不喜欢这个,因为它似乎非常脆弱。第二个问题是在一个更复杂的应用程序中将GLKViewController设置为rootViewController我得到以下结果(预计左边没有黑色区域):
There are still some issues. First of all I don't like this since it seems to be very fragile. Second problem is that in a more complex application that sets a GLKViewController as the rootViewController I get the following result (expected would be no black area on the left):
看起来状态栏没有隐藏足够早。几个手势识别器处于活动状态,在GLKViewController中单击黑色区域会产生以下日志消息:
It looks like the status bar is not hidden early enough. Several gesture recognizers are active and in the GLKViewController and clicking on the black area yields the following log message:
我还执行了各种其他更改,例如附加空的UIViewController并将我的视图添加为子视图。在这种情况下,我的视图看起来是正确的,但窗口仍然使用了错误的尺寸。
I also performed various other changes, like attaching an empty UIViewController and adding my view as a sub-view. In that case my view looks correct but the window is still using the wrong dimensions.
如果我没有覆盖视图控制器中的supportedInterfaceOrientations方法,那么一切都旋转正确。但这当然不是我想要的。
Everything rotates correct if I do not override the supportedInterfaceOrientations methods in my view controller. But that is of course not what I want.
推荐答案
从横向应用时>肖像模式UIScreen在iOS 8中具有纵向边界(仅当您在app开关面板中没有此应用时,因为iOS 8会进行一些缓存)。即使用 makeKeyAndVisible
显示窗口也不会改变它的帧。但是根据 AppViewController
可用的方向更改 [UIScreen mainScreen] .bounds
。
When you run landscape app from portrait mode UIScreen has portrait bounds in iOS 8 (only if you haven't this app in app switch panel, as iOS 8 makes some cache). Even displaying window with makeKeyAndVisible
doesn't change it's frame. But it changes [UIScreen mainScreen].bounds
according to AppViewController
avaliable orientation.
#import "AppDelegate.h"
#import "AppViewController.h"
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Portrait bounds at this point
self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
self.window.backgroundColor = [UIColor cyanColor];
self.window.rootViewController = [[AppViewController alloc] init];
[self.window makeKeyAndVisible];
return YES;
}
@end
所以让我们改变窗口的框架 [self.window makeKeyAndVisible]
So let's change window's frame after [self.window makeKeyAndVisible]
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [UIWindow new];
self.window.backgroundColor = [UIColor cyanColor];
self.window.rootViewController = [[AppViewController alloc] init];
[self.window makeKeyAndVisible];
// Here it is
self.window.frame = [UIScreen mainScreen].bounds;
return YES;
}
我认为这是iOS 8的错误。
I think that it is iOS 8 bug.
这篇关于使用横向时UIWindow的大小错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!