【问题标题】:ModalViewController transparent background iOS 8ModalViewController 透明背景 iOS 8
【发布时间】:2014-09-01 11:54:10
【问题描述】:

在 Xcode 6 中打开旧项目,发现在呈现的视图控制器中背景不透明的问题。我在下面的主题中使用了解决方案,并且到目前为止它在 ios7 中有效。 iOS : ModalView with background transparent?

请建议如何处理 iOS 8。

【问题讨论】:

标签: uiviewcontroller ios8 modalviewcontroller


【解决方案1】:

尝试将 presented 视图控制器的 modalPresentationStyle 属性设置为新的 UIModalPresentationOverCurrentContext 常量,例如

[_modalViewController setModalPresentationStyle:UIModalPresentationOverCurrentContext]

【讨论】:

  • 如果您在“segue”中更改了演示样式,则必须将其更改回默认值。否则它会给你一个黑色的背景。
  • 在代码中呢?我设置了这种演示风格,仍然得到它:/我正在调用 presentViewController
  • 如果您有标签栏,请改用全屏。
【解决方案2】:

mmccomb 的解决方案最终对我有用,但我不得不调整一些事情。希望这些细节对某人有所帮助:

1) 我必须从呈现视图控制器设置呈现视图控制器的属性,如下所示:

MyViewController *myVc = [segue destinationViewController];
[myVc setModalPresentationStyle:UIModalPresentationOverCurrentContext];

在显示的 VC 的 viewDidLoad 中设置显示样式不起作用。

2) 我使用的是故事板,我必须进入 segue 属性并将 Presentation 样式设置为 Default。设置任何其他值都会给我一个黑色背景。

【讨论】:

  • 奇怪的行为。感谢您的帮助。
  • 在 viewDidLoad 中设置演示样式对我也不起作用,必须在其他地方重置。
  • 我认为这是另一个 xcode 的错误,2) 对我有用!
  • 在 swift 中略有不同:myVc.modalPresentationStyle = .OverCurrentContext,其余与此答案完全相同! +1
【解决方案3】:

界面生成器解决方案

感谢@mmccomb 的编程解决方案。

通过以下方式实现:

  1. 在情节提要文件中创建转场(ctrl + 单击并拖动)

  2. 从下拉选项中选择模态显示

  3. 在 IB 故事板中选择模态转场 - (只是转场)

  4. Attributes Inspector 中将 Presentation 设置为 Over Current Context

我建议将 alpha/opacity 更改为 50% + 并使用较暗的背景颜色,否则您将很难区分呈现视图控制器的内容和模态呈现的内容。

【讨论】:

  • 非常感谢您的解决方案和您提供的步骤。这正是我正在寻找的。​​span>
  • @ChrisHaze,我按照您提供的这些步骤解决了我的问题,但是当关闭视图控制器时,以前的视图控制器的视图方法没有像 viewDidAppear 的 viewWillAppear 等那样调用。为什么?
【解决方案4】:

如果您在“segue”中更改了演示样式,则必须将其更改回默认值。否则它会给你一个黑色的背景。

我尝试使用带有 Segue 配置的 Storyboard,但它不起作用。

这是适用于我的代码,在 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender 方法中:

// Make sure your segue name in storyboard is the same as this line
if ([[segue identifier] isEqualToString:@"PresentStationChooserViewController"])
{
    ChooseStationViewController *controller = [segue destinationViewController];
    controller.view.backgroundColor = [UIColor colorWithWhite:0 alpha:0.7f];
    [controller setModalPresentationStyle:UIModalPresentationOverCurrentContext];
    controller.stationList = _remoteStations;
}

【讨论】:

  • 这真的很有帮助!谢谢!
【解决方案5】:

iOS8+

请看下面的代码 sn-p,它解决了我的问题,

SecondViewController *secondViewController = [[SecondViewController alloc] init];
secondViewController.modalPresentationStyle = UIModalPresentationOverCurrentContext;           
[self presentViewController:secondViewController animated:YES completion:nil];    

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-07-15
    • 2013-06-09
    • 2011-03-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多