Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is the parent frame not kept the same size after dismissViewControllerAnimated?

I've downloaded the PageControl sample code from apple. It's a project with a UIPageControl. I've changed it so that the UIScrollView has different views. For each view I want to add, I created a UINavigationController with a rootViewController as a subclass of either UIViewController or UITableViewController. Then I added into the UIScrollView, the view of the navigationController.

Let's say I'm on the first view, now. I want to show a modal view controller. When I dismiss it, the parent view controller (HomeViewController in my example) is in full screen, rather than the size it was before, which would show the page control at the bottom. The only way I can make it work properly is by resizing the frame after the dismiss, but that's a bad user experience, because it is called after the parent view controller is shown. Any ideas on how to avoid the HomeViewController frame to be resized when dismissing a modal view controller?

-(void)dismissMVC:(id)sender {
    CGRect frame = self.view.frame;
    [self dismissViewControllerAnimated:YES completion:^{
        self.view.frame = frame;
    }];    
}


- (void)loadScrollViewWithPage:(int)page
{
if (page < 0)
    return;
if (page >= kNumberOfPages)
    return;

// replace the placeholder if necessary
switch (page) {
    case Home:
    {
        HomeViewController *controller = [viewControllers objectAtIndex:page];

        if ((NSNull *)controller == [NSNull null])
        {
            controller = [[HomeViewController alloc] initWithPageNumber:page];
            [viewControllers replaceObjectAtIndex:page withObject:controller];
            [controller release];
        }

        // add the controller's view to the scroll view
        if (controller.view.superview == nil)
        {
            UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:controller];
            navController.navigationBar.barStyle = UIBarStyleBlack;
            [navController setNavigationBarHidden:YES];
            CGRect frame = scrollView.frame;
            frame.origin.x = frame.size.width * page;
            frame.origin.y = 20;
            frame.size.height = frame.size.height - 20;
            navController.view.frame = frame;
            controller.view.frame = frame;
            [controller setNavController:navController];

            [scrollView addSubview:navController.view];
            [navController release];
        }
        break;
    }
}
}
like image 305
Adriana Avatar asked Jul 03 '12 15:07

Adriana


1 Answers

When pushing veiwControllers onto a navigation stack, viewControllers who's view are not the current one being shown can be released because of low memory.

If you maintain an instance of the HomeViewController either in the appdelegate or inside the viewcontroller instance you pushed originally, you could call HomeViewController.view to force it's view to load and then give it the desired frame before you call dismissViewControllerAnimated.

like image 182
Mark Avatar answered Oct 04 '22 15:10

Mark