presentViewController black background instead of transparent

后端 未结 5 877
栀梦
栀梦 2021-01-18 03:24

I have a view that I wish to present to the user in the standard way (sliding up from the bottom of the screen). About half this view is a transparent background and the bot

相关标签:
5条回答
  • 2021-01-18 03:55

    Using SWIFT 4, just add this code to the view controller you want to have a transparent background.

    override func viewDidLoad()
    {
        super.viewDidLoad()
        self.modalPresentationStyle = .overFullScreen
        self.view.backgroundColor = UIColor.clear
    
    }
    
    0 讨论(0)
  • 2021-01-18 03:58

    As far as I know, transparent background is not supported when you presents a model view controller. Try retain the controller in your root view controller and simply add subview to the root view controller.

    0 讨论(0)
  • 2021-01-18 03:58

    I had a similar problem with the black background appearing after a short delay when creating the controller with

        Disclaimer *vc = [[Disclaimer alloc]init];
    

    What solved the problem for me was to create a corresponding object in IB and instantiate the viewcontroller using it's storyboard ID:

         Disclaimer *vc = (Disclaimer *)[self.storyboard instantiateViewControllerWithIdentifier:@"SBIDDisclaimer"];
    [self presentViewController:vc animated:YES completion:nil];
    

    I guess doing it via IB does some additional initialisations.

    0 讨论(0)
  • 2021-01-18 04:08

    This is possible, and rockybalboa provides a solution to this issue in the forum post on raywenderlich.com:

    iOS 8 UIModalPresentationCurrentContext is not transparent?

    That solution being, quoting balboa's answer, in Objective-C:

    Before iOS 8, you do this:

    [backgroundViewController setModalPresentationStyle:UIModalPresentationCurrentContext];
    [backgroundViewController presentViewController:_myMoreAppsViewController animated:NO completion:nil];
    

    in iOS 8, you have to do this:

    backgroundViewController.providesPresentationContextTransitionStyle = YES;
    backgroundController.definesPresentationContext = YES;
    [overlayViewController setModalPresentationStyle:UIModalPresentationOverCurrentContext];
    

    To supplement the above code with the Swift equivalent:

    backgroundViewController.providesPresentationContextTransitionStyle = true
    backgroundController.definesPresentationContext = true
    overlayViewController.modalPresentationStyle = .OverCurrentContext
    

    Having implemented this using Xcode 6.3 beta 3 on iOS 8.1 with Swift 1.2, it works perfectly.

    Just a comment that I viewed three different SO questions on this - the more recent now marked as duplicates - prior to finding and attempting the Ray Wenderlich forum solution.

    0 讨论(0)
  • 2021-01-18 04:15

    In the end, it looks like it's not possible for it to be transparent, I've got around this by adding this view as a subview outside of the bounds of the root view controller, and then slid it into place using an animation block. Not a lot of extra code, but it would have been nice to be able to use standard iOS behaviour to do it.

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