【问题标题】:Swift modal view background appearing black after setting it to clear将其设置为清除后,Swift 模态视图背景显示为黑色
【发布时间】:2017-06-20 21:19:29
【问题描述】:

我正在尝试以模态方式从控制器呈现自定义视图,但是当我这样做时,除了文本字段之外的所有内容的背景都会变成黑色,如 here 所示。 (the intended modal)

这似乎是一个相当普遍的问题,但是我已经实现了大多数类似问题作为答案的代码

这是我在第一个控制器中的 addEvent 方法:

@IBAction func addEvent(_ sender: Any) {
    let newEventViewController = NewEventViewController()
    newEventViewController.view.backgroundColor = UIColor.clear
    newEventViewController.view.isOpaque = false
    navigationController?.modalPresentationStyle = UIModalPresentationStyle.overCurrentContext
    navigationController?.present(newEventViewController, animated: true, completion: nil)
}

任何帮助弄清楚我缺少什么将不胜感激。如果它有所作为,我将使用单独的 xib 文件而不是故事板来创建我的应用程序。

【问题讨论】:

  • 您能提供更多信息吗?你试过self.present ...吗?还设置 self.modalPresentationStyle 通常 isOpaque 和 backgroundColor 应该设置在 viewDidLoad 但我不认为这是你的问题

标签: ios swift swift3 modalviewcontroller


【解决方案1】:

你需要设置 newEventController 的 modalPresentationStyle 而不是这样的 navigationController:

newEventViewController.modalPresentationStyle = UIModalPresentationStyle.overCurrentContext

虽然不需要,但我会从自我而不是像这样的导航控制器呈现:

self.present(newEventViewController, animated: true, completion: nil)

【讨论】:

  • 救了我!谢谢!我在想我应该将此属性添加到视图控制器'viewDidLoad' - 这显然不起作用,感谢代码示例
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-09-09
  • 1970-01-01
  • 1970-01-01
  • 2014-10-20
  • 2020-12-09
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多