我有一个视图控制器,我的视图背景需要是半透明的,并保持显示下面的视图。我已经在 nib
文件中调整了不透明度,并尝试将视图控制器推入导航堆栈和以模态方式呈现它,但加载后,先前的视图被卸载了。我该如何解决这个问题?
我有一个视图控制器,我的视图背景需要是半透明的,并保持显示下面的视图。我已经在 nib
文件中调整了不透明度,并尝试将视图控制器推入导航堆栈和以模态方式呈现它,但加载后,先前的视图被卸载了。我该如何解决这个问题?
这是您正在寻找的内容吗?
视图控制器 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)
UIView
替代视图控制器,然后调整视图的背景色的透明度。Swift 3.0代码
let addIncidentViewController = self.storyboard?.instantiateViewController(withIdentifier: "addIncidentViewController") as! AddIncidentViewController
addIncidentViewController.modalPresentationStyle = .overCurrentContext
self.present(addIncidentViewController, animated: true) {
}
并使用一些透明度设置背景颜色。
AppsDev,
这是您要找的内容吗?在此示例中,Testing1234是添加到parentViewController的标签,Me here是添加到以模态方式呈现的childViewController的标签 :)
如果您的答案是肯定的,那么以下是如何实现它的方法 :) 假设您正在使用Storyboard,我将编写答案 :) 如果您使用Xib,不用担心,所有这些属性也可以通过编程设置 :)
关键点是进行全屏幕模态演示
:)
以下是如何实现的:
在两个ViewControllers之间拖动segue :) 让segue以模态方式呈现viewController,并选择以下配置:)
现在选择将呈现secondViewController的parent ViewController,并将其背景颜色更改为白色(或任何您想要的颜色)
现在选择需要呈现的secondViewController :) 选择其View并将其alpha设置为0.5,颜色设置为clear color,如下所示
现在添加另一个View到viewController,并将其颜色设置为黑色,alpha设置为0.5或0.6,具体取决于您对阴影的需求 :)
现在添加任何您想要添加的视图组件,运行后,输出结果如上所示 :)
希望能帮到您 :)
nib
文件。 - AppsDevUIModalPresentationStyle.OverFullScreen
但是它对我不起作用 :S - AppsDevSwift3
在 viewWillAppear
方法中,您可以使用以下代码:
self.view.alpha = 0.8
在 viewWillDisappear
方法中,你可以使用以下代码:
self.view.alpha = 1.0
有两种方法可以使用透明度视图呈现ViewController。
在当前视图控制器之上
// presenting view controller
definesPresentationContext = true
// presented view controller
modalPresentationStyle = .overCurrentContext
// presented view controller
modalPresentationStyle = .overFullScreen
modalTransitionStyle = .crossDissolve
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)
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)
UINavigationController
中的视图控制器只有在通过“返回”导航弹出后才会被卸载。您能否通过添加问题的图像和您期望发生的图像来更清楚地说明您的问题? - Robotic Cat