How to present a half modal view controller over the top with iOS 7 custom transitions

后端 未结 1 1841
长发绾君心
长发绾君心 2021-01-30 06:09

How would I go about presenting a \"half view\" controller over the top of main view controller?

Requirements: - Present a second view controller that slides over the to

1条回答
  •  日久生厌
    2021-01-30 06:23

    One way to do it is to add the "half modal" controller as a child view controller, and animate its view into place. For this example, I created the "half modal" controller in the storyboard with a frame that's half the height of a 4" iPhone screen. You could use more dynamic methods to account for different screen sizes, but this should get you started.

    @interface ViewController ()
    @property (strong,nonatomic) UIViewController *modal;
    @end
    
    @implementation ViewController
    
    
    - (IBAction)toggleHalfModal:(UIButton *)sender {
        if (self.childViewControllers.count == 0) {
            self.modal = [self.storyboard instantiateViewControllerWithIdentifier:@"HalfModal"];
            [self addChildViewController:self.modal];
            self.modal.view.frame = CGRectMake(0, 568, 320, 284);
            [self.view addSubview:self.modal.view];
            [UIView animateWithDuration:1 animations:^{
                self.modal.view.frame = CGRectMake(0, 284, 320, 284);;
            } completion:^(BOOL finished) {
                [self.modal didMoveToParentViewController:self];
            }];
        }else{
            [UIView animateWithDuration:1 animations:^{
                self.modal.view.frame = CGRectMake(0, 568, 320, 284);
            } completion:^(BOOL finished) {
                [self.modal.view removeFromSuperview];
                [self.modal removeFromParentViewController];
                self.modal = nil;
            }];
        }
    }
    

    0 讨论(0)
提交回复
热议问题