ios - 当 View Controller 在本地显示并显示警报时,控件不会变暗

标签 ios cocoa-touch uiviewcontroller

当我在 View Controller 上显示 UIAlertController 时,控件按预期变暗。

但是,如果同一 View Controller 本身以模态方式呈现,则警报 Controller 的显示不会使控件变暗(两个按钮保持蓝色)。

如何让呈现的 View Controller 本身正确地处理呈现,并使它的控件变暗?

Here是一个小示例项目。相关代码在MainViewController.swift中。

最佳答案

到目前为止,我的最佳解决方法是使用自定义的 UIAlertController 子类来设置 tintAdjustmentMode 及其出现/消失动画,使用 transitionCoordinator:

/// 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

关于ios - 当 View Controller 在本地显示并显示警报时,控件不会变暗,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56418276/

相关文章:

ios - 在 SQLite 中对 RatingColumn 求和

ios - 框架在其 Info.plist 中没有 CFBundleIdentifier

ios - 处理从未附加手势的区域开始但平移到附加手势的 View 的 UIPanGesture

iphone - 另一个速度提升可能吗?

ios - 在 View Controller 之间传递数据

iphone - UIWebView 不自动调整大小以适应屏幕

objective-c - 在 UITabBarController 中为 Xcode Storyboard Segue 设置标识符,并获取指向其 View Controller 的指针

android - polymer 1.x : Going from web to mobile

cocoa - NSManagedObject 子类和 setValuesForKeysWithDictionary :

iPhone UITableViewCell 图层阴影