varun
varun

Reputation: 497

Fix orientation of UIViewController

We have added a UITabBarController programmatically in our app. We added 3 UINavigationController in UITabViewController like below code:

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];

In didSelectViewController in TabBar we added below code. This code rotates FirstViewController to portrait mode if the user is in landscape mode in any other tab and then he taps on first tab.

[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
   }
}

Our requirement:

We want to fix the orientation of FirstViewController only to portrait. It shall not rotate. Rest UIViewControllers can be rotate in both landscape and portrait. Can anyone please help?

Upvotes: 0

Views: 230

Answers (2)

KKRocks
KKRocks

Reputation: 8322

You can restrict your orientation as per your requirement using following link :

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

Upvotes: 0

pedrouan
pedrouan

Reputation: 12910

Implement this in the view controller, you want to avoid rotations on:

-(BOOL)shouldAutorotate
{
    return NO;    
}

Upvotes: 1

Related Questions