[英]Controls not dimming when view controller is presented locally and alert displayed
When I present a UIAlertController
over a view controller, the controls are dimmed as expected. 当我在视图控制器上显示UIAlertController
,控件将按预期变暗。
However, if the same view controller is itself modally presented, the display of the alert controller does not dim the controls (the two buttons remain blue). 但是,如果同一视图控制器本身是模态呈现的,则警报控制器的显示不会使控件变暗(两个按钮保持蓝色)。
How do I make a presented view controller itself handle presentations correctly, and dim its controls? 如何使呈现的视图控制器本身正确处理呈现并使其控件变暗?
Here is a small example project. 这是一个小示例项目。 The relevant code is in MainViewController.swift
. 相关代码在MainViewController.swift
。
The best workaround I have so far is to use a customized UIAlertController
subclass to set the tintAdjustmentMode
alongside its appear/disappear animations, using the transitionCoordinator
: 到目前为止,我最好的解决方法是使用一个自定义的UIAlertController
子类,使用transitionCoordinator
设置tintAdjustmentMode
及其出现/消失的动画:
/// A `UIAlertController` that can udpates its presenting view controller's `tintAdjustmentMode` code as it appears and disappears
class TintAdjustingAlertController: UIAlertController {
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
animatePresentingViewTintAdjustmentMode(tintAdjustmentMode: .dimmed, forViewControllerAtKey: .from)
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
animatePresentingViewTintAdjustmentMode(tintAdjustmentMode: .automatic, forViewControllerAtKey: .to)
}
private func animatePresentingViewTintAdjustmentMode(tintAdjustmentMode mode: UIView.TintAdjustmentMode, forViewControllerAtKey key: UITransitionContextViewControllerKey) {
transitionCoordinator?.animate(alongsideTransition: { context in
if let presentingNavigationController = context.viewController(forKey: key) as? UINavigationController {
presentingNavigationController.navigationBar.tintAdjustmentMode = mode
presentingNavigationController.viewControllers.forEach { $0.view.tintAdjustmentMode = mode }
} else if let presentingViewController = context.viewController(forKey: key) {
presentingViewController.view.tintAdjustmentMode = mode
}
}, completion: nil)
}
}
This works, but I hope not to have to pepper it throughout my code. 这行得通,但是我希望不必在我的整个代码中都使用它。 Would still love to know a) if there is a simple way to make this work as expected, or b) if this is indeed an iOS bug, is there a more elegant workaround? 还是想知道a)是否有一种简单的方法可以按预期进行此工作,或者b)如果这确实是iOS错误,是否有更优雅的解决方法?
I have also submitted a radar for this: http://www.openradar.me/radar?id=6113750608248832 我还为此提交了雷达: http : //www.openradar.me/radar?id=6113750608248832
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.