使用带有模糊背景的视图控制器

12

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


你需要在模态呈现的视图控制器中设置一个截取前一个视图控制器的屏幕截图作为背景图片。 - iSashok
UINavigationController 中的视图控制器只有在通过“返回”导航弹出后才会被卸载。您能否通过添加问题的图像和您期望发生的图像来更清楚地说明您的问题? - Robotic Cat
1
@RoboticCat 我发现在导航动画中,无论是将其推入导航栈还是以模态方式呈现,前一个视图都会显示在我所呈现的视图下方。但是当动画结束并且视图完全占据屏幕时,前一个视图就会“消失”,然后我看到了黑色背景。 - AppsDev
8个回答

23

这是您正在寻找的内容吗?

视图控制器 A -> 视图控制器 B(nib)

Swift < 3:

在视图控制器 A 中,添加以下代码行:

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

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

在视图控制器B中,使用colorWithAlphaComponent方法设置view的背景颜色:

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

Swift ≥ 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)

1
太棒了,对我有用。谢谢@Anh Pham。 - PiyushRathi

3
您可以使用 UIView 替代视图控制器,然后调整视图的背景色的透明度。

这是正确的答案。当我尝试弹出新的视图控制器时,原始视图控制器会在动画完成后立即消失。因此,我宁愿为此使用一个视图。 - Display Name

3

Swift 3.0代码

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

    }

并使用一些透明度设置背景颜色。


2

AppsDev,

这是您要找的内容吗?在此示例中,Testing1234是添加到parentViewController的标签,Me here是添加到以模态方式呈现的childViewController的标签 :)

enter image description here

如果您的答案是肯定的,那么以下是如何实现它的方法 :) 假设您正在使用Storyboard,我将编写答案 :) 如果您使用Xib,不用担心,所有这些属性也可以通过编程设置 :)

关键点是进行全屏幕模态演示 :)

以下是如何实现的:

在两个ViewControllers之间拖动segue :) 让segue以模态方式呈现viewController,并选择以下配置:) enter image description here

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

enter image description here

现在选择需要呈现的secondViewController :) 选择其View并将其alpha设置为0.5,颜色设置为clear color,如下所示

enter image description here

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

enter image description here

现在添加任何您想要添加的视图组件,运行后,输出结果如上所示 :)

希望能帮到您 :)


是的,我想要类似的东西,但我正在管理分离的nib文件。 - AppsDev
@appsdev:这不应该是问题,对吧,伙计 :) 只需按照所述的方式设置其背景颜色,最后在以编程方式加载时确保以节点方式呈现并覆盖整个屏幕 :) - Sandeep Bhandari
1
我已经设置了 UIModalPresentationStyle.OverFullScreen 但是它对我不起作用 :S - AppsDev
嗯嗯 :) 有什么问题吗????你是否将xib的背景颜色设置为透明,并在其上添加了一个黑色颜色和alpha为0.5的新视图??? 我附上的输出是执行代码的输出,应该可以正常工作 :) - Sandeep Bhandari
1
这是在Stack Overflow上针对此问题的最佳答案...我尝试了许多解决方案,最终得出了这个..谢谢老板!! - hellodear
如何更改不透明度? - Satyam

2

Swift3

viewWillAppear 方法中,您可以使用以下代码:

self.view.alpha = 0.8 

viewWillDisappear 方法中,你可以使用以下代码:

self.view.alpha = 1.0

{btsdaf} - regina_fallangi
只有当 alpha 值为 0 时,背景才应变黑。在这种情况下,它被设置为 0.8,因此背景将轻微变暗。 - Geniouse

1

有两种方法可以使用透明度视图呈现ViewController。

在当前视图控制器之上

// presenting view controller
definesPresentationContext = true

// presented view controller
modalPresentationStyle = .overCurrentContext

如果视图控制器在全屏状态下具有导航栏或选项卡。
// presented view controller
modalPresentationStyle = .overFullScreen
modalTransitionStyle = .crossDissolve

如何改变透明度? - Satyam

1
假设这样一种情况,你想在多个地方使用你的视图控制器(比如说你正在创建一个框架)。你需要将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)

0
(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)    

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接