将UIViewController呈现为具有透明背景的模态

Pat*_*sut 15 uikit swift

我试图在iOS 7和iOS 8上呈现具有透明背景的viewcontroller.只需将viewcontroller的modalPresentationStyle属性更改为FormSheet,我就可以在iOS 7.1上运行它.

我想要的是在ios7 +上通用的方法

我尝试过使用modalPresentationStyle的其他选项,如:OverCurrentContext,CurrentContext和PageSheet.

我也尝试使用modalPresentationStyle.Custom,但没有任何成功.

如果有任何帮助,我有NavigationController.

呈现视图控制器的代码:

InfoViewController *info = [[InfoViewController alloc] initWithNibName:@"InfoViewController" bundle:nil];
[self presentViewController:info animated:YES completion:nil];
Run Code Online (Sandbox Code Playgroud)

我们提供的ViewController的viewDidLoad代码(我认为它有相关的部分):

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view.
    self.modalPresentationStyle = UIModalPresentationStyle.PageSheet
}
Run Code Online (Sandbox Code Playgroud)

我正在使用swift和Xcode 6.这里分别是我现在和我想要的截图:

在此输入图像描述在此输入图像描述

这是一个示例代码:https://github.com/pbassut/TransBackgroundViewController

Pat*_*sut 19

对于那些在呈现UIViewController之前仍然存在此问题的人,将呈现的UIViewController的modalPresentationStyle设置为.Custom,它将在iOS 8(Xcode 6.1)上运行.也就是说,你应该在呈现UIViewController中设置它


Bas*_*awy 12

我尝试过这个解决方案,它适用于iOS 7和8:

if (UIDevice.currentDevice().systemVersion.integerValue >= 8)
{
    //For iOS 8
    presentingViewController.providesPresentationContextTransitionStyle = true;
    presentingViewController.definesPresentationContext = true;
    presentedViewController.modalPresentationStyle = UIModalPresentationOverCurrentContext;
}
else
{
    //For iOS 7
    presentingViewController.modalPresentationStyle = UIModalPresentationCurrentContext;
}
Run Code Online (Sandbox Code Playgroud)

注意:请注意' presentsViewController '和' presentsViewController '之间的区别.


gho*_*ron 6

我没有代码就能实现这个目标:

  1. 在故事板中,在呈现视图控制器上(即在您想要透明的那个之前),转到属性检查器.在该选项卡上,有"定义上下文"和"提供上下文"的复选框.检查那些.

  2. 在segue上,将其设置为"Present Modally".

  3. 在目标/显示的视图控制器上,转到属性选项卡.在"Presentation"下拉列表中,选择"Over Current Context".

  4. 在目标视图控制器上,将其视图设置为具有清晰的背景.

  5. 在目标视图控制器上,打下2个UIViews:一个是你的"透明",另一个是你的"内容".将透明的一个设置为您喜欢的任何alpha,并将所有垃圾放入"内容"中.


Tyl*_*230 6

以上都不适合我.为了在iOS 10上实现这一点,我简单地说: presented.modalPresentationStyle = .overFullScreen presenting.present(presented, animated: true) 然后将呈现的视图控制器的根视图设置为透明或半透明颜色.不要更改其alpha或其所有子视图将具有相同的alpha.