简体   繁体   中英

presentViewController black background instead of transparent

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 bottom half has some input fields (imagine the way the keyboard pops up). When I call [self presentViewController] on the rootViewController, it slides the view up, but then about half a second later, where the view used to be transparent it is replaced with black instead. This happens with both presentViewController and presentModalViewController. How to change this behaviour?

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.

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.

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

}

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM