Search code examples
iosviewcontrolleruimodalpresentationformsh

Why modal presentation is empty


I have two view controllers. In the first one I have a button and when it is clicked I want to show the other view controller in the right of the screen to login like modal present form sheet.

For the moment I have the second view controller with a label but when I click it appears empty, I don't know way because I put a label inside, and of course in the middle of the screen.

LoginViewController *loginController = [[LoginViewController alloc] init];
loginController.modalPresentationStyle = UIModalPresentationFormSheet;
[self presentViewController:loginController animated:YES completion:nil];
loginController.view.superview.frame = CGRectMake(0, 0, 300, 1000);
loginController.view.superview.center = self.view.center;

Any suggestion to see the label? To change the position?


Solution

  • I think it would be best to use the custom container controller methods to do this. In the example code I have below, I made a login controller that was the size of a form sheet (540 x 600) and slid it in from the right onto the main view of ViewController so that it was centered vertically, and up against the right side.

    In ViewController.m, I have this:

    -(IBAction)showLogin:(id)sender {
        LoginController *login = [self.storyboard instantiateViewControllerWithIdentifier:@"Login"];
        login.view.frame = CGRectMake(768, 202, 540, 600);//centered vertically and offscreen to the right
        [self addChildViewController:login];
        [self.view addSubview:login.view];
        [UIView animateWithDuration:.5 animations:^{
            login.view.frame = CGRectMake(228, 202, 540, 600);
        } completion:^(BOOL finished) {
            [login didMoveToParentViewController:self];
        }];
    }
    

    And to remove the view, I have this in the LoginController.m:

    -(IBAction)goBackToMain:(id)sender {
        [UIView animateWithDuration:.5 animations:^{
            self.view.frame = CGRectMake(768, 202, 540, 600);
        } completion:^(BOOL finished) {
            [self.view removeFromSuperview];
            [self removeFromParentViewController];
        }];
    }
    

    After Edit:

    If you want to remove the login controller from a button in ViewController, you can do it like this:

    -(IBAction)goBackToMain:(id)sender {
            LoginController *login = self.childViewControllers.lastObject;
            [UIView animateWithDuration:.5 animations:^{
                login.view.frame = CGRectMake(768, 202, 540, 600);
            } completion:^(BOOL finished) {
                [login.view removeFromSuperview];
                [login removeFromParentViewController];
            }];
        }