【问题标题】:Presenting a view controller with dim background呈现背景暗淡的视图控制器
【发布时间】:2016-10-05 18:00:19
【问题描述】:

我有一个视图控制器,我需要它的视图背景是半透明的并继续显示下面的视图。我已经调整了nib 文件中的不透明度,并尝试将视图控制器推入导航堆栈并以模态方式呈现它,但是在加载时,前一个视图被卸载。我该如何解决这个问题?

【问题讨论】:

  • 您需要对前一个视图控制器进行截图并将其设置为模态呈现的视图控制器作为背景图像
  • UINavigationController 中的视图控制器在通过导航“返回”弹出之前不会被卸载。通过添加问题图片和您期望发生的情况图片,您能否更清楚地了解您的问题?
  • @RoboticCat 我看到前一个视图显示在我在顶部呈现的视图下方,而导航动画,无论是我将其推送到导航堆栈还是模态呈现,但是当此动画结束,视图完全占据屏幕,前一个视图“消失”,然后我看到黑色背景

标签: ios swift uiviewcontroller opacity presentviewcontroller


【解决方案1】:

您可以使用UIView 代替视图控制器。 然后调整view背景颜色的alpha。

【讨论】:

  • 这是正确的答案。当我尝试弹出一个新的 VC 时,原始的 VC 会在动画结束后立即消失。所以我宁愿只为此使用视图。
【解决方案2】:

应用开发,

这就是你要找的朋友吗???这里 Testing1234 是添加到 parentViewController 的标签 这里是我 是添加到 childViewController 的标签,以模态方式呈现 :)

如果你的答案是肯定的,你可以这样做:) 假设你正在使用故事板,我会写答案:) 如果你使用 Xib,别担心所有这些属性也可以通过编程方式设置 :)

这里的重点是做modal presentationoverfull screen:)

你可以这样做:)

在两个 ViewController 之间拖动一个 segue :) 使 segue 以模态方式呈现 viewController 并选择以下配置 :)

现在选择您的父 ViewController,它将显示 secondViewController 并将其背景颜色更改为白色(或您想要的任何颜色)

现在选择需要显示的 secondViewController :) 选择它的 View 并将其 alpha 设置为 0.5 并将颜色设置为清除颜色,如下所示

现在将 anotherView 添加到 viewController 中,将其颜色设置为黑色,并将 alpha 设置为 0.5 或 0.6,具体取决于您对阴影的需要:)

现在添加任何你想在它上面添加的视图组件运行,你将看到如上所示的输出:)

希望对你有帮助:)

【讨论】:

  • 是的,我想要这样的东西,但我正在管理分开的 nib 文件
  • @appsdev : 应该不是问题,不是朋友 :) 只需按照提到的方式设置它的背景颜色,最后当你以编程方式加载它时,确保你以节点方式和全屏显示它:)跨度>
  • 我设置了UIModalPresentationStyle.OverFullScreen,但它对我不起作用:S
  • hmmm :) 有什么问题????您是否将 xib 的背景颜色设置为清除颜色并在其顶部添加了一个新视图,黑色和 alpha 为 0.5 ????我附加的输出是执行代码的输出应该可以正常工作:)
  • 这是这个问题的堆栈溢出的最佳答案......我尝试了很多解决方案,终于来到了这个......谢谢你的老板!
【解决方案3】:

这就是你要找的吗?

查看控制器 A -> 查看控制器 B(笔尖)

斯威夫特

在 View Controller A 中,添加以下代码行:

let viewControllerB = ViewControllerB(nibName: "ViewControllerB", bundle: nil)
viewControllerB.modalPresentationStyle = .OverFullScreen

presentViewController(viewControllerB, animated: true, completion: nil)

并在 View Controller B 中,使用colorWithAlphaComponent 方法设置view 的背景颜色:

view.backgroundColor = UIColor.blackColor().colorWithAlphaComponent(0.7)        

斯威夫特≥3:

查看控制器 A:

let viewControllerB = ViewControllerB(nibName: "ViewControllerB", bundle: nil)
viewControllerB.modalPresentationStyle = .overFullScreen

present(viewControllerB, animated: true, completion: nil)

查看控制器 B:

view.backgroundColor = UIColor.black.withAlphaComponent(0.7)

【讨论】:

  • 太棒了..为我工作。 .谢谢@Anh Pham
【解决方案4】:

Swift 3.0 代码

let addIncidentViewController = self.storyboard?.instantiateViewController(withIdentifier: "addIncidentViewController") as! AddIncidentViewController
addIncidentViewController.modalPresentationStyle = .overCurrentContext
self.present(addIncidentViewController, animated: true) {

    }

并使用一些 alpha 设置背景颜色。

【讨论】:

    【解决方案5】:

    Swift3

    viewWillAppear 下可以使用:

    self.view.alpha = 0.8 
    

    viewWillDisappear 下你可以使用:

    self.view.alpha = 1.0
    

    【讨论】:

    • 这个答案有没有可能只是让背景变黑? OP提到应该仍然可以看到视图,但是当我更改视图的alpha时,它只是黑色。
    • 只有当 alpha 值为 0 时,背景才会变黑。在这种情况下,它设置为 0.8,因此背景会稍微变暗。
    【解决方案6】:

    (Swift 3) 更好的解决方案是从 xib 加载视图,然后将其添加到控制器,如下所示:

       let supportView =  Bundle.main.loadNibNamed("YourXIB", owner: nil, options: nil)?[0] as! YourView
    
        let viewController = UIViewController()
        viewController.view = supportView
        viewController.modalPresentationStyle = .overCurrentContext
    
        present(viewController, animated: true, completion: nil)e
    

    然后添加一些透明度:

        viewController.view.backgroundColor = UIColor.blackColor().colorWithAlphaComponent(0.7)    
    

    【讨论】:

      【解决方案7】:

      有两种方法可以为 ViewController 呈现不透明视图。

      当前视图控制器

      // presenting view controller
      definesPresentationContext = true
      
      // presented view controller
      modalPresentationStyle = .overCurrentContext
      

      如果视图控制器具有全屏导航或标签栏。

      // presented view controller
      modalPresentationStyle = .overFullScreen
      modalTransitionStyle = .crossDissolve
      

      【讨论】:

      • 如何更改不透明度?
      【解决方案8】:

      想象一下,您想在多个地方使用您的视图控制器(假设您正在创建框架)。您需要将modalPresentationStyle 设置为.overCurrentContext,但您不想在每次创建视图控制器的新实例时都设置它。

      您想将其设置在将要显示的视图控制器的覆盖 init

      override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: Bundle?) {
          super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
          modalPresentationStyle = .overCurrentContext
      }
      
      required init?(coder aDecoder: NSCoder) {
          fatalError("init(coder:) has not been implemented")
      }
      

      然后在此控制器的viewDidLoad() 中,只需将背景颜色改为较浅的黑色:

      override public func viewDidLoad() {
          super.viewDidLoad()
          view.backgroundColor = UIColor.black.withAlphaComponent(0.6)
      }
      

      最后,当你需要展示你的视图控制器时,只需初始化它并展示它

      let viewController = SecondViewController(nibName: "SecondViewController", bundle: nil)
      present(viewController, animated: true)
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-02-24
        • 1970-01-01
        • 2015-02-20
        • 2018-06-24
        • 2017-09-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多