gpt4 book ai didi

ios - 如何在 tabbarcontroller 中旋转一个 View ?

转载 作者:行者123 更新时间:2023-11-29 11:04:01 25 4
gpt4 key购买 nike

我觉得我的问题在 iOS6: supportedInterfaceOrientations not working (is invoked but the interface still rotates) 中得到了解答,但我还不够了解如何实现响应。

我正在使用 iOS6 编写一个选项卡式应用程序,我希望其中一个 View Controller 旋转,而其余 View Controller 保持不动。我知道我需要使用 supportedInterfaceOrientations,但我不知道如何或在何处使用它,而且我无法破译有关它的 Apple 文档。 UITabBarController 被设置为 AppDelegate 中的 Root View Controller ,如果相关的话。

在此先感谢您的帮助。

最佳答案

这个问题困扰了我一个星期。我遇到了完全相同的情况,只希望也能旋转一个 View 。在选项卡式应用程序中绕过它的方法是注册方向通知。在您的 TabController(和任何其他 super View )和您要旋转的 VC 中为 shouldAutoRotate 返回 NO。然后在 viewWillAppear 中注册通知:

[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(orientationChanged:)name:UIDeviceOrientationDidChangeNotification
object:[UIDevice currentDevice]];

被调用的函数将如下所示:

- (void) orientationChanged:(NSNotification *)note
{
UIDevice * device = note.object;
switch(device.orientation)
{
case UIDeviceOrientationPortrait:
break;

case UIDeviceOrientationPortraitUpsideDown:
break;
case UIDeviceOrientationLandscapeLeft:
if(viewHasAppeared==true){
[self performSegueWithIdentifier:@"firstToLandscape" sender:self];}
break;
case UIDeviceOrientationLandscapeRight:
if(viewHasAppeared==true){
[self performSegueWithIdentifier:@"firstToLandscape" sender:self];}
break;
default:
break;
};
}

-在 ViewDidAppear 中放置一个 bool 值,因为在 View 实际出现之前您无法继续。

现在,如果你在横向模式下切换到的 View 不是选项卡式 View Controller ,那么最好的处理方法是只允许在该 View 中进行自动旋转,然后在 willAnimate 中你只需关闭 VC,如下所示:

-(void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
if(UIInterfaceOrientationIsPortrait(toInterfaceOrientation))
{
[self dismissViewControllerAnimated:YES completion:nil];
}
}

如果它是一个选项卡式 View ,那么也只需在该 View 中注册方向更改。但是,请确保在离开 View 时删除 orientationChange 通知。如果你不这样做,那么你也会从其他 viewControllers 切换到横向。

当您离开去另一个选项卡时,您需要实现 tabBarController 委托(delegate)并确保像这样移除观察者:

-(void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
{
if(![NSStringFromClass([viewController class])isEqualToString:@"FirstViewController"])
{
[[NSNotificationCenter defaultCenter]removeObserver:self name:UIDeviceOrientationDidChangeNotification object:[UIDevice currentDevice]];
}

}

关于ios - 如何在 tabbarcontroller 中旋转一个 View ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14467508/

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