开发者

Losing rotation support after programmatically adding UITabBarController to UIWindow

My application starts off with nothing but a UIWindow. I programmatically add a view controller to self.window in application:didFinishLaunchingWithOptions:.

myViewController = [[UIViewController alloc] init:...];
...

[self.window addSubview:myViewController.view];
[self.window makeKeyAndVisible];

At the same time i kick off a background process:

[NSThread detachNewThreadSelector:@selector(startupOperations) toTarget:self withObject:nil];

The startupOperations look something like this:

NSAutoreleasePool *threadPool = [[NSAutoreleasePool alloc] init];

// Load data
开发者_如何学编程...

// When your done, call method on the main thread
[self performSelectorOnMainThread:@selector(showMainViewController) withObject:nil waitUntilDone:false];

// Release autorelease pool
[threadPool release];

showMainViewController removes myViewController, creates a UITabBarController and sets it as the window's main view:

[self.myViewController.view removeFromSuperview];
self.myViewController = nil;

tabBarController = [[UITabBarController alloc] init];
...

[self.window addSubview:tabBarController.view];
[self.window makeKeyAndVisible];

Questions:

All the view controllers are returning YES for shouldAutorotateToInterfaceOrientation:. Rotation works fine for myViewController but as soon as the tabBarController is made visible, rotation stops working and interface appears in Portrait. What's the reason behind this behavior?

Also, in iOS 4.x, UIWindow has rootViewController property. What's the role of this property? The new templates use rootViewController instead of [self.window addSubview:...]. Why is that?


Pretty strange. I tried and simulate your "view flow" in a simple tab bar based project and autorotation effectively works after removing the initial controller and adding the tab bar controller's view as a subview.

The only condition I found where it did not work is when self.window did contain a second subview that I did not remove. Could you check at the moment when you execute

 [self.window addSubview:tabBarController.view];

what is self.window.subview content?

If that does not help, could you share in your question how you initialize the UITabBarController and UITabBar?

As to your second question, as you say rootViewController is the root controller for all the views that belong to the window:

The root view controller provides the content view of the window. Assigning a view controller to this property (either programmatically or using Interface Builder) installs the view controller’s view as the content view of the window. If the window has an existing view hierarchy, the old views are removed before the new ones are installed.

(Source)

You can also use that, but take care of assigning it already in applicationDidFinishLaunching, otherwise, if you "manually" add a subview and later change this property, it will not remove the subview you explicitly added.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜