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

按下UIViewController(主视图)后,当其按索引0导航到基于tabBarViewController时,用户可以随时使用导航栏中的“后退”按钮从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。

根据我的问题,我的项目处于人像模式,我只希望控制器自动旋转单视图。以下是步骤,对我有帮助。

1-在App Delegate.h中

@property (assign, nonatomic) BOOL shouldRotate;


2-在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 A转到View ControllerB。ViewController是自动旋转的,则在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/

10-10 23:15