I created a very basic iPad application using the “Window-based Application” template. Unfortunately, the provided view will not rotate when I rotate the simulator.
Furthermore, when I add a view controller and/or a UINavigationController to the base window, they won’t rotate either. I’ve implemented
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return YES;
}
in the view controller I added … but still, can’t see any rotation.
Am I missing something?
To orchestrate autorotation, your application’s window looks for its top-most subview that’s managed by a view controller. The window calls
shouldAutorotateToInterfaceOrientation:on this view controller, and transforms the view controller’s view as directed.This is why a window based application doesn’t autorotate out-of-the-box. The default template doesn’t come with a view controller already set up for you. To take advantage of autorotation, you must create a custom
UIViewControllersubclass, edit itsshouldAutorotateToInterfaceOrientation:method as appropriate, and add that view controller’s view as a direct subview of your window (or add the view controller to a container like aUINavigationController, and add that container view controller’s view as a subview of the window). This is typically done inapplication:didFinishLaunchingWithOptions:.You state that “when I add a view controller and/or a UINavigationController to the base window, they won’t rotate either,” and that you’ve implemented
shouldAutorotateToInterfaceOrientation:, and that it still won’t rotate. This should work if you’ve configured everything properly. You probably made some small mistake in this process, but I can’t say for sure what it is without seeing your project. Here are some things to check:If you’ve defined your custom view controller in a NIB, make sure you remembered to change its Class name on the Identity Inspector in IB.
Make sure you’re adding the view controller’s view directly to the window, and that you don’t have some other view acting as a wrapper between your window and your view controller’s view.
Make sure you’re not trying to add any subviews to your Window object in IB. You should be adding its subview in
application:didFinishLaunchingWithOptions:.If you’ve defined your custom view controller in a NIB, make sure its view appears nested under the View Controller in IB. i.e. Don’t try to connect some other view to the view controller’s
viewoutlet.Those are some shots in the dark. Hopefully something in this answer will help you find your mistake. If not, try creating a view-based application and compare its default configuration to your window-based application. Maybe then your mistake will jump out at you.