UITabBar 内的单个 UIViewController 自动旋转

Single UIViewController Auto-rotation inside UITabBar

嗨,我的 TabBarViewController 层次结构是这样的。所有 ViewControllers 附加 tabbar 都没有 navigationController.

UIViewController(主页视图),当推送它导航到 tabBar based ViewController at index 0, User Can come from tabBarViewControllers to home viewController 随时使用导航栏中的后退按钮。

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

我使用了这种基于 Tabbar 的方法 ViewController,因为 Tabbar 不是主页 ViewController

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

我尝试了很多东西,比如 THIS,但没有成功。

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

根据我的问题,我的项目处于纵向模式,我只想要单视图控制器自动 rotation.Below 是对我有帮助的步骤。

1 - 在应用程序中 Delegate.h

@property (assign, nonatomic) BOOL shouldRotate;

2 - 在应用程序中 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 - 技巧部分是

如果您完成上述所有步骤,如果您从当前视图控制器(横向模式)返回,您的视图控制器将自动旋转。以前的视图控制器将横向自动旋转,并且会保持链状。

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

如果你要从视图控制器 A 转到视图控制器 B。视图控制器是自动旋转的,那么在视图控制器 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;
 }