簡體   English   中英

當本地顯示視圖控制器並顯示警報時,控件不會變暗

[英]Controls not dimming when view controller is presented locally and alert displayed

當我在視圖控制器上顯示UIAlertController ,控件將按預期變暗。

但是,如果同一視圖控制器本身是模態呈現的,則警報控制器的顯示不會使控件變暗(兩個按鈕保持藍色)。

如何使呈現的視圖控制器本身正確處理呈現並使其控件變暗?

是一個小示例項目。 相關代碼在MainViewController.swift

到目前為止,我最好的解決方法是使用一個自定義的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)
    }
}

這行得通,但是我希望不必在我的整個代碼中都使用它。 還是想知道a)是否有一種簡單的方法可以按預期進行此工作,或者b)如果這確實是iOS錯誤,是否有更優雅的解決方法?

我還為此提交了雷達: http : //www.openradar.me/radar?id=6113750608248832

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM