Present Modal View in iOS 6

2019-04-13 11:42发布

I want to display a modal view but I have two problems:

  1. There is no presentModalViewController in iOS 6 and I have to use presentViewController which is just display my second ViewController like a modal segue not a modal window without full screen option,

  2. My second problem is how can I show the modal window from UICollectionViewController. I tried to use presentViewController but it just works with ViewController not CollectionViewController.

The best example of what I want to do is this (Instagram). How they made this modal window? Is it because it's still working with older iOS versions and it's not iOS 6 yet? Or there is another way to display modal window like this from UICollectionViewController?

Thanks

enter image description here

3条回答
劫难
2楼-- · 2019-04-13 12:05

As you are saying that presentViewController is only works with UIViewController not UICollectionViewController.

Then simply import UIViewController class in header file of CollectionViewController as displayed below:

#import <UIKit/UIKit.h>

@class UIViewController;

@interface MyCVController : UICollectionViewController

@end
查看更多
男人必须洒脱
3楼-- · 2019-04-13 12:18

I would take a look at the docs for UIContainerView which is used for displaying a view controller as a child of another view controller in a similar way to a non-fullscreen modal presentation.

查看更多
The star\"
4楼-- · 2019-04-13 12:25

If I understood you correctly what you are trying to achieve is to present one of your ViewControllers over the parent and still see the parent ViewController in the background.

First solution:

UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
    UIViewController *vc = [storyboard instantiateViewControllerWithIdentifier:@"SecondViewController"];
    vc.view.backgroundColor = [UIColor clearColor];
    self.navigationController.modalPresentationStyle = UIModalPresentationCurrentContext; //self.modalPresentationStyle might do a better job here
    [self presentViewController:vc animated:NO completion:nil];

Make sure your SecondViewController content is smaller than your current ``ViewController` and that you can see the background color of it in StoryBoard \ xib. The background color will be clear and will create the transparency effect.

Second Solution:

Create a Container (iOS 6 and up if you are planning to use the Storyboard IB, lower than that will let you create Containers but only progrematicly).

Container

Set the container size 3/4 of the parent size and connect the second viewcontroller to it. Instead of segueing \ pushing to your second viewcontroller you can just

myContainer.alpha = 1; 

to show it on screen.

查看更多
登录 后发表回答