【问题标题】:Fix orientation of UIViewController修复 UIViewController 的方向
【发布时间】:2016-09-30 06:39:51
【问题描述】:

我们在应用程序中以编程方式添加了一个 UITabBarController。我们在 UITabViewController 中添加了 3 个 UINavigationController,如下代码:

self.tabBarController = [[UITabBarController alloc] init];
self.tabBarController.tabBar.tintColor = [UIColor colorWithRed:36.0/255.0 green:179.0/255.0 blue:125.0/255.0 alpha:1.0];

FirstViewController *VC1 = [[FirstViewController alloc] initWithNibName:@"FirstViewController" bundle:nil];
UINavigationController *navControllerFirst = [[UINavigationController alloc]initWithRootViewController:VC1];
UITabBarItem *item1 = [[UITabBarItem alloc] initWithTitle:@"First" image:[UIImage imageNamed:@"First"] tag:0];
navControllerFirst.tabBarItem = item1;

SecondViewController *VC2 = [[SecondViewController alloc]initWithNibName:@"SecondViewController" bundle:nil];
UINavigationController * navControllerSecond = [[UINavigationController alloc]initWithRootViewController:VC2];
UITabBarItem *item2 = [[UITabBarItem alloc] initWithTitle:@"Second" image:[UIImage imageNamed:@"Second"] tag:0];
navControllerSecond = item2;

ThirdViewController *callTransactionsNew=[[ThirdViewController alloc]initWithStyle:UITableViewStyleGrouped];
UINavigationController * navControllerThird = [[UINavigationController alloc]initWithRootViewController:callTransactionsNew];
UITabBarItem *item3 = [[UITabBarItem alloc] initWithTitle:@"Third" image:[UIImage imageNamed:@"Third"] tag:0];
navControllerThird = item3;

NSArray* controllers = [NSArray navControllerFirst, navControllerSecond, navControllerThird,nil];
self.tabBarController.viewControllers = controllers;
self.tabBarController.delegate = self;
self.tabBarController.view.frame = self.view.frame;

[self.tabBarController willMoveToParentViewController: self];
[self addChildViewController: self.tabBarController];
[self.view addSubview: self.tabBarController.view];
[self.tabBarController didMoveToParentViewController:self];

在 TabBar 的 didSelectViewController 中,我们添加了以下代码。如果用户在任何其他选项卡中处于横向模式,然后他点击第一个选项卡,则此代码将 FirstViewController 旋转到纵向模式。

[UIViewController attemptRotationToDeviceOrientation];
if([theTabBarController.viewControllers indexOfObject:viewController] == 0) {
    SEL sel = NSSelectorFromString(@"setOrientation:");
    if([[UIDevice currentDevice] respondsToSelector:sel]) {
      #pragma clang diagnostic push
      #pragma clang diagnostic ignored "-Warc-performSelector-leaks"
      [[UIDevice currentDevice] performSelector:sel withObject:(__bridge id)((void*)UIInterfaceOrientationPortrait)];
      #pragma clang diagnostic pop
   }
}

我们的要求:

我们只想将 FirstViewController 的方向固定为纵向。它不应旋转。 Rest UIViewControllers 可以横向和纵向旋转。 有人可以帮忙吗?

【问题讨论】:

  • 是的,我们是。一定有什么办法。我已经看到其他应用程序这样做了。

标签: ios objective-c uiviewcontroller uitabbarcontroller


【解决方案1】:

在视图控制器中实现这一点,您希望避免旋转

-(BOOL)shouldAutorotate
{
    return NO;    
}

【讨论】:

    【解决方案2】:

    您可以使用以下链接根据您的要求限制您的方向:

    How to allow only single UIViewController to rotate in both Landscape and Portrait direction?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-06
      • 2016-04-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多