简体   繁体   English

当本地显示视图控制器并显示警报时,控件不会变暗

[英]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.

相关问题 呈现的模态视图控制器未像表单一样显示 - Presented Modal view controller not displayed like a Form 显示警报控制器时,Scrollview高度会发生变化 - Scrollview Height Changes when Alert Controller is presented 如何防止在出现警报时显示视图控制器的 inputAccessoryView? - How to prevent inputAccessoryView of a view controller from being shown when an alert is presented? 呈现视图控制器时,UILabel作为UIBarButtonItem.customView的子视图不会变暗 - UILabel as subview of UIBarButtonItem.customView not dimming when presenting view controller 在根视图上显示另一个视图时,警报视图中心会更改 - Alert view center changes when another view is presented on root view API 调用成功完成 SWIFT 后不显示警报视图控制器 - Alert view controller not presented once API call successfully completed SWIFT 是否需要从顶视图控制器显示警报视图? - Do alert views need to be presented from the top view controller? 出现操作表时,View Controller不会自动旋转 - View controller is not autorotating when Action Sheet is presented 出现模态视图 controller 时暂停 animation - Pause animation when modal view controller is presented 检测何时关闭呈现的视图控制器 - Detect when a presented view controller is dismissed
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM