4

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.

enter image description here

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;
Matrosov Oleksandr
  • 25,505
  • 44
  • 151
  • 277

2 Answers2

1

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;
}
Mark Kryzhanouski
  • 7,251
  • 2
  • 22
  • 22
Alexander Merchi
  • 1,495
  • 13
  • 11
  • Alexander, this is helped you? – Alexander Merchi Apr 06 '13 at 18:55
  • it seems work, thanks, but the problem with frame. if in my case there was 20 point on left side, right now there is 20 points on bottom. – Matrosov Oleksandr Apr 07 '13 at 12:05
  • ok, i have change my xib for thirdviewcontroller and remove status bar there. but I also moved my content on 20 points from to to bottom and right now it works. thanks – Matrosov Oleksandr Apr 07 '13 at 12:11
  • also I have tested it and it works even without rotation delegates. I just set [UIApplication sharedApplication].statusBarHidden = YES; before present view controller and then [UIApplication sharedApplication].statusBarHidden = NO; after present line – Matrosov Oleksandr Apr 07 '13 at 12:22
0

Your SecondViewController frame is (0, 20; 300, 480) instead of (0, 0; 320, 480), because of the status bar on the top of your screen. So either you can remove the status bar from "Attributes inspector" by setting the value "none" or you can customize the view frame in

-(void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
      if (UIInterfaceOrientationIsLandscape(orientation)) {
          //Landscape orientation code
      }else {
          //portrait orientation code   
      }
}

method.

x4h1d
  • 6,042
  • 1
  • 31
  • 46
  • thanks for response very useful answer as I think. I have tried add this code to my base view controller and check the frame. it prints right value but the problem with 20 pixel still there. Also right now I have not use XIB for second view controller and can't adjust what you suggested with status bar. I have updated my question with your code please take a look at that, maybe I have miss something. But in any case great solution. Any other idea what's wrong with this. – Matrosov Oleksandr Apr 04 '13 at 16:05
  • did you use storyboard? then go to the storyboard and select the scene of the second view controller (not view :) ) go to the "Attributes inspector" (third from the right) and set the value of status bar to none. please tell me whats' the change. If you have created the view from code the only option is to adjust the code in the method mentioned above. I'm afraid there is no other way. – x4h1d Apr 05 '13 at 05:42
  • No I don't use story board – Matrosov Oleksandr Apr 05 '13 at 10:00
  • I use this method but still have this problem. I can add XIB and adjust it. – Matrosov Oleksandr Apr 05 '13 at 10:01