Ath*_*rge 5 objective-c rotation uitabbarcontroller ios landscape-portrait
我需要我的应用程序兼容iPad和iPhone.它有一个tabbarController作为rootViewController.
在iPad中我需要它在Landscape和Portrait上都可用.在iPhone中虽然我需要rootView是Portrait本身,但我确实有一些viewsControllers,它们在tabbarController上呈现,需要在横向和Portrait中可用(例如用于播放Youtube视频的viewController).所以我按如下方式锁定tabbarController的旋转(在UITabbarController子类中).
# pragma mark - UIRotation Methods
- (BOOL)shouldAutorotate{
return (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad);
}
- (NSUInteger)supportedInterfaceOrientations{
return (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) ? UIInterfaceOrientationMaskAll : UIInterfaceOrientationMaskPortrait;
}
Run Code Online (Sandbox Code Playgroud)
我打算做的是通过锁定rootviewController(tabbarController)的旋转,我锁定tabbarController中的所有VC(仅在iPhone上),并且tabbarController顶部显示的视图可以根据设备方向旋转.
问题
一切都按预期工作,直到应用程序在iPhone中的风景中启动.在横向模式下启动时,应用程序默认为横向并以横向模式启动应用程序,这不是预期的.即使设备方向为横向,它也应在纵向模式下启动.由于我关闭iPhone的自动旋转,应用程序继续在横向本身导致错误.我尝试使用此方法强制应用程序在应用程序中以纵向方式启动:didFinishLaunchingWithOptions:
#pragma mark - Rotation Lock (iPhone)
- (void)configurePortraitOnlyIfDeviceIsiPhone{
if ((UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone))
[[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationPortrait];
}
Run Code Online (Sandbox Code Playgroud)
问题仍然存在.我已经允许在info.plist上为iPad和iPhone提供SupportInterfaceOrientaions键的所有方向选项,因为我需要应用程序才能在iPhone中使用,即使只有几个viewControllers.如果我可以以某种方式强制该应用程序以纵向方向启动,即使设备方向是横向,也可以解决该问题.如果错误,请纠正我,如果没有,任何帮助使应用程序以纵向模式启动将不胜感激.
谢谢
这就是我设法让它发挥作用的方法。在AppDelegate.m中,我添加了这个方法。
- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window{
//if iPad return all orientation
if ((UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad))
return UIInterfaceOrientationMaskAll;
//proceed to lock portrait only if iPhone
AGTabbarController *tab = (AGTabbarController *)[UIApplication sharedApplication].keyWindow.rootViewController;
if ([tab.presentedViewController isKindOfClass:[YouTubeVideoPlayerViewController class]])
return UIInterfaceOrientationMaskAllButUpsideDown;
return UIInterfaceOrientationMaskPortrait;
}
Run Code Online (Sandbox Code Playgroud)
每次显示视图时,此方法都会检查方向并根据需要更正方向。我返回 iPad 的所有方向,而不返回 iPhone 的所有方向,除了要呈现的视图(应该旋转的视图,YouTubeVideoPlayerViewController)被保留。
在 tabbarController 子类中,
# pragma mark - UIRotation Methods
- (BOOL)shouldAutorotate{
return YES;
}
- (NSUInteger)supportedInterfaceOrientations{
return (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) ? UIInterfaceOrientationMaskAll : UIInterfaceOrientationMaskPortrait;
}
Run Code Online (Sandbox Code Playgroud)
问题是,当我们向 shouldAutoRotate 返回 no 时,应用程序将忽略所有旋转更改通知。它应该返回 YES,以便它旋转到supportedInterfaceOrientations中描述的正确方向
我想这就是我们应该如何满足这个要求,而不是像许多帖子所说的那样将旋转指令传递给各自的视图控制器。这是 Apple 推荐的使用容器的一些优点,这样我们就不必在容器中的每个视图上编写旋转指令。