I have next view controllers:
FirstViewController
SecondViewController
ThirdViewController
The goal is: present ThirdViewController via SecondViewController.
In FirstViewController I present SecondViewController using method below:
[self presentModalViewController:secondViewController animated:NO];
When the SecondViewController is loaded I present ThirdViewController in -viewDidAppear:
delegate callback method:
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
[self presentModalViewController:thirdViewController animated:NO];
}
So, as I think the scheme for present view controllers is already known for all of us. Maybe the design of the code is not good, but I have made this solution and have problem described below.
When the ThirdViewController is loaded I have issues with 20 points (but just for iOS 4.3, other versions work good).
I have attached file that shows my issue.
As you can see on the left side I have unneeded offset. After rotate screen this issue disappear.
When I print ThirdViewController views frame it shows me frame = (0 0; 480 300). Maybe the issue in SecondViewControllers view.
About rotation. I have implemented these methods below in each controllers:
- (BOOL)shouldAutorotate
{
return YES;
}
- (NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskLandscape;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
if (interfaceOrientation==UIInterfaceOrientationLandscapeLeft || interfaceOrientation==UIInterfaceOrientationLandscapeRight)
return YES;
return NO;
}
(UPDATE:) Yes I have found the issue right now and the SecondViewController frame = (0 20; 300 480); but I don't understand why.
I add this method for check current orientation:
-(void) getCurrentOrientation{
UIInterfaceOrientation orientation = [[UIDevice currentDevice] orientation];
if(orientation == UIInterfaceOrientationPortrait){
NSLog(@"portrait");
} else if(orientation == UIInterfaceOrientationLandscapeRight) {
NSLog(@"LandscapeRight");
} else if(orientation == UIInterfaceOrientationLandscapeLeft) {
NSLog(@"LandscapeLeft");
}
}
and check it in -viewDidAppear:
method and it show that current orientation is portrait instead of landscape mode that I setup in shouldAutorotateToInterfaceOrientation
callback.
Also, I have found that for iPad iOS 4.3 it works good.
I did all test on iPhone Simulator.
I have added this code to my base view controller, but it still does not work for me.
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
if (UIInterfaceOrientationIsLandscape(self.interfaceOrientation)) {
//Landscape orientation code
if (IS_IPAD) {
[self.view setFrame:CGRectMake(0, 0, 1024, 748)];
}
else if (IS_IPHONE) {
[self.view setFrame:CGRectMake(0, 0, 480, 300)];
}
else if (IS_IPHONE_5) {
[self.view setFrame:CGRectMake(0, 0, 568, 300)];
}
NSLog(@"landscape");
NSLog(@"%@", [NSValue valueWithCGRect:self.view.frame]);
}else {
//portrait orientation code
NSLog(@"portrait");
}
}
(WORK UPDATE:)
I fixed this problem using this code below:
[UIApplication sharedApplication].statusBarHidden = YES;
if (self.thirdViewController) self.thirdViewController = nil;
self.thirdViewController = [[ThirdViewController alloc] initWithNibName:@"ThirdViewController"] bundle:nil];
[self presentModalViewController:self.self.thirdViewController animated:NO];
[UIApplication sharedApplication].statusBarHidden = NO;
Alexander, could you please try below code. It is work in 4.3 (add to SecondViewController):
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
[super willRotateToInterfaceOrientation:toInterfaceOrientation duration:duration];
[UIApplication sharedApplication].statusBarHidden = YES;
}
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
AMThirdViewController* th = [[AMThirdViewController alloc] initWithNibName:nil bundle:nil];
[self presentModalViewController:th animated:NO];
[UIApplication sharedApplication].statusBarHidden = NO;
}