简体   繁体   English

presentViewController黑色背景而不是透明

[英]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. 当我在rootViewController上调用[self presentViewController]时,它会向上滑动视图,但是大约半秒后,视图过去是透明的,而是用黑色代替。 This happens with both presentViewController and presentModalViewController. 这与presentViewController和presentModalViewController都会发生。 How to change this behaviour? 如何改变这种行为?

This is possible, and rockybalboa provides a solution to this issue in the forum post on raywenderlich.com : 这是可能的,rockybalboa在raywenderlich.com上的论坛帖子中提供了解决此问题的方法

iOS 8 UIModalPresentationCurrentContext is not transparent? iOS 8 UIModalPresentationCurrentContext不透明?

That solution being, quoting balboa's answer, in Objective-C: 该解决方案在Objective-C中引用巴尔博亚的答案:

Before iOS 8, you do this: 在iOS 8之前,您执行此操作:

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

in iOS 8, you have to do this: 在iOS 8中,您必须这样做:

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

To supplement the above code with the Swift equivalent: 用Swift等价物补充上面的代码:

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. 在iOS 8.1和Swift 1.2上使用Xcode 6.3 beta 3实现了这一点,它完美地运行。

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. 只是评论我在查找和尝试Ray Wenderlich论坛解决方案之前, 已经查看了三个不同的SO问题 - 现在标记为重复

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. 使用SWIFT 4,只需将此代码添加到您想要具有透明背景的视图控制器即可。

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. 不是很多额外的代码,但能够使用标准的iOS行为来做这件事会很不错。

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: 解决这个问题的方法是在IB中创建一个相应的对象,并使用它的storyboard ID实例化viewcontroller:

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

I guess doing it via IB does some additional initialisations. 我想通过IB进行一些额外的初始化。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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