views:

1000

answers:

5

I'm trying to build an iPhone application that has two subviews in the main window. Each view has its own UIViewController subclass associated with it. Also, within each controller's implementation, I've added the following method:

-(BOOL)shouldAutorotateToInterfaceOrientation:
(UIInterfaceOrientation)interfaceOrientation {
        return YES;
}

Thus, I would expect both of the views to respond to changes in orientation. However, this is not the case. Only the first view added to the app's main window responds to orientation. (If I swap the order the views are added, then only the other view responds. In other words, either will work--but only one at a time.)

Why is this? Is it not possible to handle the orientation changes of more than one view?

Thanks!

EDIT: Someone else had this question, so I'm copying my solution here:

I was able to address this issue by providing a root view and a root view controller with the method "shouldAutoRotate..." and adding my other views as subviews to the root view. The subviews inherit the auto-rotating behavior, and their associated view controllers shouldn't need to override "shouldAutoRotate..."

A: 

you can try to implement this method in the class, which view contains both your viewController's views

Morion
+2  A: 

You need to override the shouldAutorotateToInterfaceOrientation method on all three controllers you have.

Eduardo Scoz
I only have two UIViewController subclasses. Perhaps that's the issue?
Tom
A: 

Hi, I'm seeing the exact same issue ( 2 view controllers associated with 2 views that are each added to the main window....each view controller implements YES for shouldAutorotate...only the first view added will rotate, the other will not ).

Was there ever a resolution for fixing this? I'm not sure what the first reply regarding "on all three controllers" means.

Anyone have a solution?

Thanks!

Bryan
+1  A: 

Finally going to post my solution here:

Basically, it came down to having a "root" UIViewController. In this, I have the method:

-(BOOL)shouldAutorotateToInterfaceOrientation:
(UIInterfaceOrientation)interfaceOrientation {
        return YES;
}

I also added the views I wanted to auto-rotate as subviews to this root UIViewContoller's UIView. These subviews also have view controllers but do not override shouldAutorotateToInterfaceOrientation. In fact, adding this method to those view controllers seems to have no effect whatsoever. Only the root view contoller's shouldAutorotateToInterfaceOrientation seems to work and all subviews get rotated properly.

Tom
A: 

OK, but what to do if some of the subviews required programmatic repositioning of UI-elements? Do you put this all (for all views) into the one didRotateFromInterfaceOrientation: method of the root-view controller?

SpaceAce
Basically, for views that required programmatic positioning of its UI elements, I created a subclass of UIView and overrode the "- (void)layoutSubviews" method. This is triggered whenever "setNeedsLayout" is called, if I recall correctly.
Tom