gpt4 book ai didi

ios - UITabBar 内的单个 UIViewController 自动旋转

转载 作者:行者123 更新时间:2023-11-29 00:41:11 27 4
gpt4 key购买 nike

你好,我的 TabBarViewController 层次结构是这样的。所有附有 tabbarViewControllers 都没有 navigationController

UIViewController (Home View),当推送它导航到 tabBar based ViewController at index 0, User Can come back from tabBarViewControllers 随时使用导航栏中的后退按钮回到首页 viewController

UITabBarViewController (BaseViewController)
-ViewController0,(NO Navigation ViewController)
-ViewController1 (NO Navigation ViewController)
-ViewController2 (NO Navigation ViewController)
-ViewController3 (NO Navigation ViewController)
-ViewController4 (NO Navigation ViewController)

我使用了这种基于 TabbarViewController 方法,因为 Tabbar 不是 Home ViewController

我只想在纵向和横向中自动旋转 ViewController2。我的项目仅处于纵向模式。

我尝试了很多类似 THIS 的东西,但没有得到。

最佳答案

嗨,经过大量研究,我发现了什么,无论是 Tabbar 还是 UIVicontroller

根据我的问题,我的项目处于纵向模式,我只想单 View Controller 自动旋转。以下是对我有帮助的步骤。

1 - In App Delegate.h

@property (assign, nonatomic) BOOL shouldRotate;

2 - In App Delegate.m

- (UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(nullable UIWindow *)window  NS_AVAILABLE_IOS(6_0) __TVOS_PROHIBITED
{
_shouldRotate = [[NSUserDefaults standardUserDefaults]boolForKey:@"rotateKey"];
NSLog(@"Did I get to InterfaceOrientation \n And the Bool is %d",_shouldRotate);

if (self.shouldRotate == YES){
return UIInterfaceOrientationMaskAll;
}else{
return UIInterfaceOrientationMaskPortrait;
}
}

3 - 现在哪个 UIViewController,您想要自动旋转

-(void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:YES];
BOOL rotate = YES;
[[NSUserDefaults standardUserDefaults]setBool:rotate forKey:@"rotateKey"];
[[NSUserDefaults standardUserDefaults]synchronize];
}

-(BOOL)shouldAutorotate
{
return YES;
}

4 - 技巧部分是

如果您执行上述所有步骤,如果您从当前 View Controller (横向模式)返回,您的 View Controller 将自动旋转。之前的 View Controller 将在横向中自动旋转,并且它将保持链式。

所以,为了避免这种情况,

如果您要从 View Controller A 转到 View Controller B。 View Controller 是自动旋转的,那么在 View Controller A 中-

5 - 使用此代码 -

-(void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:YES];

BOOL rotate = NO;
[[NSUserDefaults standardUserDefaults]setBool:rotate forKey:@"rotateKey"];
[[NSUserDefaults standardUserDefaults]synchronize];
}
-(BOOL)shouldAutorotate
{
return NO;
}

- (BOOL)shouldAutorotateToInterfaceOrientation: (UIInterfaceOrientation)toInterfaceOrientation
{
return (toInterfaceOrientation == UIInterfaceOrientationPortrait);
}

#if __IPHONE_OS_VERSION_MAX_ALLOWED < 90000
- (NSUInteger)supportedInterfaceOrientations
#else
- (UIInterfaceOrientationMask)supportedInterfaceOrientations
#endif
{
dispatch_async(dispatch_get_main_queue(), ^{
// UI Updates

});
return UIInterfaceOrientationMaskPortrait;
}

关于ios - UITabBar 内的单个 UIViewController 自动旋转,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39483957/

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