由于出现问题,我无法在主 UIWindow
中显示内容,因此我已向应用程序添加了第二个 UIWindow
。我知道不建议这样做,但我有点被迫走这条路。
无论如何,我已经创建了第二个 UIWindow
,但我现在尝试将其设置为响应方向更改,因为与主窗口不同,它不会自动执行此操作。问题是我对 iOS 特定编码缺乏经验,对此我有点不知所措。
这是我到目前为止所得到的:
// Create the new UIWindow
mWindow = [[UIWindow alloc] initWithFrame: CGRectMake(0, 0, size.height, size.width)];
mWindow.windowLevel = UIWindowLevelStatusBar + 1.0f;
[mP3NMoreGamesWindow setRootViewController:mMyViewController];
MyViewController定向特定功能:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations.
NSString * supported_orientation = [Utilities GetDeviceOrientation];
if ([supported_orientation isEqualToString:@"landscape"] && (interfaceOrientation == UIInterfaceOrientationLandscapeLeft || interfaceOrientation == UIInterfaceOrientationLandscapeRight))
{
return YES;
}
else if ([supported_orientation isEqualToString:@"portrait"] && (interfaceOrientation == UIInterfaceOrientationPortrait ||
interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown))
{
return YES;
}
return NO;
}
- (BOOL)shouldAutorotate
{
//returns true if want to allow orientation change
return YES;
}
- (NSUInteger)supportedInterfaceOrientations
{
NSString * supported_orientation = [Utilities GetDeviceOrientation];
if ([supported_orientation isEqualToString:@"landscape"])
{
return UIInterfaceOrientationMaskLandscape;
}
else
{
return UIInterfaceOrientationMaskPortrait;
}
}
我有什么遗漏的吗?我的印象是窗口的 rootviewcontroller
接收方向更改通知?
此外,是否可以在创建时设置 UIWindow
的方向?例如,如果我创建 UIWindow
时当前方向是横向,我可以将 UIWindowfi
设置为横向而不是默认的纵向吗?
谢谢。
最佳答案
解决方案位于应用程序委托(delegate)中。您可以分别处理每个窗口支持的方向:
- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
{
// check, which window is asking and return the mask
return UIInterfaceOrientationMaskAllButUpsideDown;
}
关于ios - 第二个 UIWindow 中的方向更改,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21836153/