@Mangesh-Vyas has some good information but doesn't quite answer the question (as I read it). The reason that the views that Twitter (among other apps) presents can rotate, when the tabbar or navbar cannot, is because they are presented modally. Views which are presented modally aren't constrained to the allowable orientations of the controller which presented them.
Edit:
Perhaps you are expecting a stock UIViewController
to rotate. If so then you will be disappointed, as only (to the best of my knowledge) custom subclasses can be made to rotate to arbitrary interfaceOrientations. The following code will do the trick:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return YES;
}
Edit 2:
Here's the code that makes the whole thing go:
AppDelegate.m
#import "RotateViewController.h"
@interface AppDelegate () {
UINavigationController* nc;
}
- (void)pushVC;
- (void)openVC;
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
UIViewController* vc = [[UIViewController alloc] init];
vc.view.backgroundColor = [UIColor blueColor];
vc.title = @"Root";
UIButton* b = [UIButton buttonWithType:UIButtonTypeRoundedRect];
b.frame = CGRectMake(50, 150, 220, 40);
[b.titleLabel setText:@"Push VC"];
[b addTarget:self action:@selector(pushVC) forControlEvents:UIControlEventTouchUpInside];
[vc.view addSubview:b];
b = [UIButton buttonWithType:UIButtonTypeRoundedRect];
b.frame = CGRectMake(50, 200, 220, 40);
[b.titleLabel setText:@"Modal VC"];
[b addTarget:self action:@selector(openVC) forControlEvents:UIControlEventTouchUpInside];
[vc.view addSubview:b];
nc = [[UINavigationController alloc] initWithRootViewController:vc];
UITabBarController* tc = [[UITabBarController alloc] init];
[tc addChildViewController:nc];
// uncomment this line to see this work in a nav controller
// [self.window setRootViewController:nc];
[self.window setRootViewController:tc];
// Override point for customization after application launch.
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
return YES;
}
- (void)openVC {
RotateViewController* rc = [[RotateViewController alloc] init];
[nc presentModalViewController:rc animated:YES];
}
- (void)pushVC {
RotateViewController* rc = [[RotateViewController alloc] init];
[nc pushViewController:rc animated:YES];
}
RotateViewController is a stock subclass of UIViewController
with the shouldAutorotate
function from above.
Nota bene UITabBarControllers
can rotate to a given interface orientation if, and only if, every one of their child view controllers returns YES
for that orientation. UINavigationViewControllers
can rotate to a given interface orientation if their top view controller returns YES
for that interface orientation.