Oli Griffiths
Oli Griffiths

Reputation: 61

iOS 7 Modal presents below status bar

I'm trying to present a view modally from within a view controller, that is contained within another view controller (eg, a tab bar controller, containing a view controller, that presents another view controller modally).

Hierarchy like:

View Controller 1
    view 1 (belonging to view controller 1)
         view 2 (belonging to view controller 2)
            -> presented view 3 (belonging to view controller 3)

The problem I am having is that on an ipad, the presented view controller appears below the status bar, leaving the presenting view controller visible behind the status bar. I want the view to fill the whole screen and sit behind the status bar.

The strange thing is, if I present the modal from view controller 1, it works fine, if I try and present it from view controller 2, I get this issue.

The other strange thing is that if I run the same code in the iphone simulator, there is no problem, works as expected, but if I run it in the ipad sim, this issue is present.

For example:

see screenshots

The red view is the view belonging to view controller 2, the green is view controller 3.

As you can see, on the iphone, view controller 3 covers the whole screen, whereas on the ipad it sits below the status bar.

To test this, create a new single view universal project in xcode, and add the following code to the viewDidAppear method in the created view controller and run in the iphone and ipad simulator:

-(void)viewDidAppear:(BOOL)animated
{
    UIViewController *vc1 = [UIViewController new];
    vc1.view.backgroundColor = [UIColor redColor];
    [self.view addSubview:vc1.view];

    UIViewController *vc2 = [UIViewController new];
    vc2.view.backgroundColor = [UIColor greenColor];

    [vc1 presentViewController:vc2 animated:YES completion:nil];
}

Anyone got any thoughts? I've tried all combinations of the new edgesForExtendedLayout properties and so on according to apples transition guide. I have to avoid adding anything to vc2.

Created 2 sample projects here that show the difference between a tabbar application presenting a modal and a non-tabbar project presenting a modal. Both are setup in the same way, with 3 view controllers.

It seems odd that it would differ between iphone and ipad.

Oli

Upvotes: 3

Views: 3095

Answers (2)

Oli Griffiths
Oli Griffiths

Reputation: 61

I found the answer to this.

When adding the child view controllers (the ones that will present the modal) to a parent view controller, you must tell the parent that the child view controllers as indeed children, this affects the modal context when

  [self presentViewController:animated:completion:]

Is called.

You do this by calling:

[self addChildViewController:viewController]

When you add the sub view controller.

If you remove a view controller from the parent you must call:

[viewController removeFromParentViewController];

This fixes the issue above.

As a note, the modal display context between an iPhone and an iPad is different, iPhones will always display a modal view controller full screen, no matter what, on an iPad, the presenting view controller needs to know what context to display the modal view controller in (basically who the parent view controller is).

Hope this helps.

Upvotes: 3

paulrehkugler
paulrehkugler

Reputation: 3271

That is how modal views are presented on the iPad. It's designed to be a feature to present depth to the user, not a bug.

As far as iPhone modals are concerned, they currently always take up the full screen. You should not consider this behavior to be permanent; Apple may decide to leave the status bar visible in future OS/SDK updates for the iPhone.

However, what you describe in your post (presenting a vc modally from a vc presented modally) does occasionally cover the status bar (I've noticed that it does this if you present a Full Screen modal from a Page Sheet modal, this behavior occurs). I believe that this behavior is an iOS7 bug and will be fixed in later SDK or OS updates.

Upvotes: 0

Related Questions