Transparent Background with a Modal UIViewController

我的未来我决定 提交于 2019-11-28 05:25:50
pix0r

Funny, I was just doing the same thing yesterday. Unfortunately it seems to be impossible. Once the modal view controller is in place, the previous view becomes hidden. See this previous question on the topic.

You can still use the view controller and NIB files you have set up - here's my sample code

- (void)showUpgrade {
    [self.upgradeVC viewWillAppear:NO];
    [self.view addSubview:self.upgradeVC.view];
    [self.upgradeVC viewDidAppear:NO];
}

- (void)hideUpgrade {
    [self.upgradeVC viewWillDisappear:NO];
    [self.upgradeVC.view removeFromSuperview];
    [self.upgradeVC viewDidDisappear:NO];
}

- (UpgradeViewController *)upgradeVC {
    if (_upgradeVC == nil) {
        _upgradeVC = [[UpgradeViewController alloc] initWithNibName:[NSString stringWithFormat:@"UpgradeView_%@", self.deviceType] bundle:nil];
        _upgradeVC.delegate = self;
    }
    return _upgradeVC;
}

You will need to store a reference to the parent view controller in the modal view controller so that you can access the -hide method. I did this through a delegate.

It would also be easy to add some animation to -show and -hide if you want it to animate up from the bottom of the screen - I was just too lazy to do this.

iOS 8 added the UIModalPresentationOverFullScreen presentation style. Set this as the presented view controller’s modalPresentationStyle. For more advanced needs, look into creating a custom presentation controller.

There is now a way to achieve this using iOS7 custom transitions :

MyController * controller = [MyController new];
[controller setTransitioningDelegate:self.transitionController];
controller.modalPresentationStyle = UIModalPresentationCustom;
[self controller animated:YES completion:nil];

To create your custom transition, you need 2 things :

  • A TransitionDelegate object (implementing <UIViewControllerTransitionDelegate>)
  • An "AnimatedTransitioning" object (implementing <UIViewControllerAnimatedTransitioning>)

You can find more informations on custom transitions in this tutorial : http://www.doubleencore.com/2013/09/ios-7-custom-transitions/

Try this:

ViewController *vc = [[ViewController alloc] init];
[vc setModalPresentationStyle:UIModalPresentationOverCurrentContext];
[self presentViewController:vc animated:YES completion:nil];

Have you tried looping over the Modal View Controller's subviews and setting the background color to clear for every view? This is a DFS recursive function.

- (void)setBackgroundToClearForView:(UIView *)view {
    if ([view subviews]) {
        for (UIView *subView in [view subviews]) {
            [self setBackgroundToClearForView:subView];
        }
    }

    if ([view respondsToSelector:@selector(setBackgroundColor:)]) {
        [view performSelector:@selector(setBackgroundColor:)
                   withObject:[UIColor clearColor]];
    }
}

To use it call:

[self setBackgroundToClearForView:self.view];

in viewDidLoad.

This will do the trick.. Try this one.

// for clear color or you can easily adjust the alpha here
 YourVC *vc=[[YourVC alloc]initWithNibName:@"YourVC" bundle:nil] ;
 vc.view.backgroundColor = [UIColor clearColor];
 self.modalPresentationStyle = UIModalPresentationCurrentContext;
 [self presentViewController:vc animated:NO completion:nil];

So that the view will be full screen unlike UIModalPresentationFormSheet..

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!