Reputation: 1
I am building a simple app with two View Controllers, I am testing the code using the iPhone Simulator, everything seem to be working fine. The problem happens when I rotate from Portrait to Landscape or from Landscape to portrait. This is the logic of the app, the app always launched in Portrait, I have a button to on the first View to Switch from View1 to View2. On View2 I have another button to switch from View2 back to View1. Let say, I am in Portrait mode, I switch from View1 to View2, then rotate the iPad (in the simulator) from Portrait to Landscape, when I switch back from View2, i.e to go back to View1. View1 screen/view is displayed in Portrait with View2 screen displayed in the background, ie part of View2 is displayed in the background, I guess because View1 was originally in Portrait mode.
The question is.. Has anyone had this issue before, if so, any code to fix this issue, secondly, how can I identify in the code which orientation the device is and which orientation the view is in.
This method is to switch to View 2:
-(IBAction) switchToView2: (id) sender {
SecondViewController *myViewController = [[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle:nil];
[self.view addSubview: myViewController.view];
[UIView commitAnimations];
}
This method is to switch back to View1:
-(IBAction) switchBackToView1:(id) sender {
[self.view removeFromSuperview];
[UIView commitAnimations];
}
Upvotes: 0
Views: 634
Reputation: 11
From your code:
[self.view addSubview: myViewController.view];
It makes me believe that your 2 views is a subview of myViewController.view. which explains why they're both showing at the same time. it would make sense to have seperate view controllers for different views.
Upvotes: 1
Reputation: 3572
Yes, that would happen if you dont put any orientation change handling in your code. Check out this guide on how you can set your view to automatically or manually handle adjustment on orientation change.
Or if you do have handling already, then it may be because of how you are adding/removing your views. For better handling, I think you should try the UINavigationController way of managing views. i.e., instead of addSubview:
and removeFromSuperview:
, you should use pushViewController:animated:
and popViewController:animated:
instead.
And yes, as Yoos said, [UIView commitAnimations]
is not needed in your code above.
Hope this helps.
Upvotes: 0
Reputation: 11
First i think
[UIView commitAnimations];
is not necessary :
From Apple
commitAnimations Marks the end of a begin/commit animation block and schedules the animations for execution.
second in Interface Builder have you set the property of your controller to landscape ?
Hope this Help (sorry for my bad English)
Upvotes: 0