繁体   English   中英

presentViewController黑色背景而不是透明

[英]presentViewController black background instead of transparent

我希望以标准方式向用户呈现(从屏幕底部向上滑动)。 大约一半的视图是透明背景,下半部分有一些输入字段(想象键盘弹出的方式)。 当我在rootViewController上调用[self presentViewController]时,它会向上滑动视图,但是大约半秒后,视图过去是透明的,而是用黑色代替。 这与presentViewController和presentModalViewController都会发生。 如何改变这种行为?

这是可能的,rockybalboa在raywenderlich.com上的论坛帖子中提供了解决此问题的方法

iOS 8 UIModalPresentationCurrentContext不透明?

该解决方案在Objective-C中引用巴尔博亚的答案:

在iOS 8之前,您执行此操作:

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

在iOS 8中,您必须这样做:

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

用Swift等价物补充上面的代码:

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

在iOS 8.1和Swift 1.2上使用Xcode 6.3 beta 3实现了这一点,它完美地运行。

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

据我所知,当您呈现模型视图控制器时,不支持透明背景。 尝试将控制器保留在根视图控制器中,只需将子视图添加到根视图控制器即可。

使用SWIFT 4,只需将此代码添加到您想要具有透明背景的视图控制器即可。

override func viewDidLoad()
{
    super.viewDidLoad()
    self.modalPresentationStyle = .overFullScreen
    self.view.backgroundColor = UIColor.clear

}

最后,看起来它不可能是透明的,我通过将这个视图添加为根视图控制器边界之外的子视图,然后使用动画块将其滑动到位来解决这个问题。 不是很多额外的代码,但能够使用标准的iOS行为来做这件事会很不错。

在创建控制器时,我在短暂的延迟后出现了类似的黑色背景问题

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

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

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

我想通过IB进行一些额外的初始化。

暂无
暂无

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

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