gpt4 book ai didi

iOS9 supportedInterfaceOrientationsForWindow 停止调用

转载 作者:可可西里 更新时间:2023-11-01 04:40:58 25 4
gpt4 key购买 nike

我想在横向模式下显示 1 或 2 个 UIViewcontroller,而其他 Controller 在纵向模式下。为此,我在 AppDelegate 中实现了这个功能。

-(UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
{
return self.orientation;
}

在 AppDelegate.h 中,方向是:

@property (nonatomic, assign) UIInterfaceOrientationMask orientation;

在我需要横向方向的 UIViewcontroller(s) 中。我把这段代码放在

-(void)viewWillAppear:(BOOL)animated
{
self.appDelegate.orientation = UIInterfaceOrientationMaskLandscape;
}

-(void)viewWillDisappear:(BOOL)animated
{
self.appDelegate.orientation = UIInterfaceOrientationMaskPortrait;
}

但是,当我转到“LandscapeViewController”时它工作正常,我返回时它工作正常,我再次转到“LandscapeViewController”它正常,然后当我回来时,supportedInterfaceOrientationsForWindow 方法停止被调用。有什么理由吗?还是我在做一些奇怪的事情?

最佳答案

如果您使用的是 UITabBarController,请为 UITabBarController 创建一个自定义类并将此代码放在那里

-(UIInterfaceOrientationMask) supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskPortrait;
}

并根据您的需要将其放置在您的 View Controller 中。

将此代码放在 appDelegate 中

-(UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
{

return UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight;

id rootViewController = [self topViewControllerWithRootViewController:window.rootViewController];

if (rootViewController != nil)
{
if ([rootViewController respondsToSelector:@selector(canRotate)])
{
return UIInterfaceOrientationMaskLandscape;
}
}
return UIInterfaceOrientationMaskPortrait;
}

-(UIViewController*) topViewControllerWithRootViewController:(id)rootViewController
{

if (rootViewController == nil)
{
return nil;
}

if ([rootViewController isKindOfClass:[UITabBarController class]])
{
UITabBarController *selectedTabBarController = rootViewController;
return [self topViewControllerWithRootViewController:selectedTabBarController.selectedViewController];
}
else if ([rootViewController isKindOfClass:[UINavigationController class]])
{
UINavigationController *selectedNavController = rootViewController;
return [self topViewControllerWithRootViewController:selectedNavController.visibleViewController];
}
else
{
UIViewController *selectedViewController = rootViewController;
if (selectedViewController.presentedViewController != nil)
{
return [self topViewControllerWithRootViewController:selectedViewController.presentedViewController];
}
}
return rootViewController;
}

并将它放在你的 View Controller 中

-(void)canRotate
{

}

关于iOS9 supportedInterfaceOrientationsForWindow 停止调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39335190/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com