ios 呈现背景暗淡的视图控制器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/37652159/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Presenting a view controller with dim background
提问by AppsDev
I have a view controller which view's background I need to be translucent and keep showing the view below. I've adjusted the opacity in the nib
file and tried both pushing the view controller into a navigation stack and presenting it modally, but when loaded, the previous view is unloaded. How could I solve this?
我有一个视图控制器,该视图的背景需要半透明并继续显示下面的视图。我已经调整了nib
文件中的不透明度,并尝试将视图控制器推入导航堆栈并以模态方式呈现它,但是在加载时,前一个视图被卸载。我怎么能解决这个问题?
回答by Anh Pham
Is this what you're looking for?
这是你要找的吗?
View Controller A -> View Controller B (nib)
视图控制器 A -> 视图控制器 B(笔尖)
Swift < 3:
斯威夫特 < 3:
In View Controller A, add the following line of code:
在视图控制器 A 中,添加以下代码行:
let viewControllerB = ViewControllerB(nibName: "ViewControllerB", bundle: nil)
viewControllerB.modalPresentationStyle = .OverFullScreen
presentViewController(viewControllerB, animated: true, completion: nil)
And in View Controller B, set the background color of view
with colorWithAlphaComponent
method:
在 View Controller B 中,设置view
withcolorWithAlphaComponent
方法的背景颜色:
view.backgroundColor = UIColor.blackColor().colorWithAlphaComponent(0.7)
Swift ≥ 3:
斯威夫特≥3:
View Controller A:
视图控制器 A:
let viewControllerB = ViewControllerB(nibName: "ViewControllerB", bundle: nil)
viewControllerB.modalPresentationStyle = .overFullScreen
present(viewControllerB, animated: true, completion: nil)
View Controller B:
视图控制器 B:
view.backgroundColor = UIColor.black.withAlphaComponent(0.7)
回答by krish
Swift 3.0 Code
Swift 3.0 代码
let addIncidentViewController = self.storyboard?.instantiateViewController(withIdentifier: "addIncidentViewController") as! AddIncidentViewController
addIncidentViewController.modalPresentationStyle = .overCurrentContext
self.present(addIncidentViewController, animated: true) {
}
And set background color with some alpha.
并使用一些 alpha 设置背景颜色。
回答by tuledev
You can use UIView
instead of a view controller.
Then adjust the alpha of view's background color.
您可以使用UIView
代替视图控制器。然后调整视图背景颜色的 alpha。
回答by Sandeep Bhandari
AppsDev,
应用开发,
Is this what you are looking for buddy ??? Here Testing1234is a label added to parentViewController Me hereis a label added to childViewController which is presented modally :)
这就是你要找的哥们???这里Testing1234是添加到 parentViewController 的标签我这里是添加到 childViewController 的标签,它以模态呈现:)
If your answer is yes, Here is how you can do it :) Assuming you are uisng storyboard I'll write the answer :) If you are using Xib dont worry all these properties can be set programmatically as well :)
如果您的答案是肯定的,那么您可以这样做:) 假设您正在使用故事板,我会写下答案:) 如果您使用的是 Xib,请不要担心所有这些属性也可以通过编程方式设置:)
The main point here is to do the modal presentation
overfull screen
:)
这里的重点是做modal presentation
overfull screen
:)
Here is how you can do it :)
这是您可以做到的方法:)
Drag a segue between your two ViewControllers :) Make the segue to present the viewController modally and select the following configurations :)
在你的两个 ViewControllers 之间拖一个 segue :) 使 segue 以模态呈现 viewController 并选择以下配置:)
Now select your parent ViewController which will present the secondViewController and change its background color to white (or whatever color you want)
现在选择您的父 ViewController,它将呈现 secondViewController 并将其背景颜色更改为白色(或您想要的任何颜色)
Now select your secondViewController which needs to be presented :) Select its View and set its alpha to 0.5 and color to clear color as shown below
现在选择需要显示的 secondViewController :) 选择它的 View 并将其 alpha 设置为 0.5 并将颜色设置为清除颜色,如下所示
Now add anotherView to the viewController set its color to black and alpha to 0.5 or 0.6 depending on your need for shade :)
现在将 anotherView 添加到 viewController 将其颜色设置为黑色,将 alpha 设置为 0.5 或 0.6,具体取决于您对阴影的需要:)
Now add whatever the view components you want to add on top of it run and you will se the out put as shown above :)
现在添加任何你想在它上面添加的视图组件运行,你将看到如上所示的输出:)
Hope it helps :)
希望能帮助到你 :)
回答by Jone
There are two ways to present ViewController with opacity view.
有两种方法可以使用不透明视图来呈现 ViewController。
Over current viewcontroller
过电流视图控制器
// presenting view controller
definesPresentationContext = true
// presented view controller
modalPresentationStyle = .overCurrentContext
If view controller has navigation or tabbar over fullscreen.
如果视图控制器在全屏上有导航或标签栏。
// presented view controller
modalPresentationStyle = .overFullScreen
modalTransitionStyle = .crossDissolve
回答by Geniouse
Swift3
斯威夫特3
Under viewWillAppear
you can use:
在viewWillAppear
你可以使用:
self.view.alpha = 0.8
And under viewWillDisappear
you can use:
在viewWillDisappear
你可以使用:
self.view.alpha = 1.0
回答by Fares Ben Hamouda
(Swift 3) Better solution is to load the view from xib and then add it to the controller like so :
(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
And then add some transparency :
然后添加一些透明度:
viewController.view.backgroundColor = UIColor.blackColor().colorWithAlphaComponent(0.7)
回答by Robert Dresler
Imagine situation, that you want to use your view controller in multiple places (let's say you're creating framework). You need to set modalPresentationStyle
to .overCurrentContext
, but you don't want to set it every time you create new instance of view controller.
想象一下,你想在多个地方使用你的视图控制器(假设你正在创建框架)。您需要设置modalPresentationStyle
为.overCurrentContext
,但您不想每次创建视图控制器的新实例时都设置它。
You want to set it inside overridden init
of view controller which will be presented
您想将其设置在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")
}
then in viewDidLoad()
of this controller just background color to lighter shade of black:
然后在viewDidLoad()
这个控制器中只是背景颜色为较浅的黑色阴影:
override public func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = UIColor.black.withAlphaComponent(0.6)
}
Finally, when you need to present your view controller, just initialize it and present it
最后,当你需要展示你的视图控制器时,只需初始化它并展示它
let viewController = SecondViewController(nibName: "SecondViewController", bundle: nil)
present(viewController, animated: true)