presentViewController black background instead of

2019-02-21 21:14发布

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?

标签: ios ios5 ios6
5条回答
forever°为你锁心
2楼-- · 2019-02-21 21:40

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

}
查看更多
【Aperson】
3楼-- · 2019-02-21 21:49

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.

查看更多
神经病院院长
4楼-- · 2019-02-21 21:57

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.

查看更多
冷血范
5楼-- · 2019-02-21 21: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.

查看更多
我欲成王,谁敢阻挡
6楼-- · 2019-02-21 22:03

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.

查看更多
登录 后发表回答