推动视图控制器时快速背景颜色

use*_*088 3 transition uiviewcontroller swift

我有两个白色的视图控制器。当我尝试推送第二个视图控制器时,我注意到灰色背景(基本上它在转换过程中改变了 alpha 值)。有什么技巧可以禁用这种淡入淡出吗?我只希望我的背景是白色的在此处输入图片说明

Cas*_*sey 6

使用 aUIViewControllerAnimatedTransitioning肯定是“正确”的方式,但似乎您真的不想使用它。

如果你想“破解”它,那么 swizzling 是要走的路。

这是UIView防止_UIParallaxDimmingView显示基础类的扩展。

extension UIView {
    static func preventDimmingView() {
        guard let originalMethod = class_getInstanceMethod(UIView.self, #selector(addSubview(_:))), let swizzledMethod = class_getInstanceMethod(UIView.self, #selector(swizzled_addSubview(_:))) else { return }
        method_exchangeImplementations(originalMethod, swizzledMethod)
    }

    static func allowDimmingView() {
        guard let originalMethod = class_getInstanceMethod(UIView.self, #selector(addSubview(_:))), let swizzledMethod = class_getInstanceMethod(UIView.self, #selector(swizzled_addSubview(_:))) else { return }
        method_exchangeImplementations(swizzledMethod, originalMethod)
    }

    @objc func swizzled_addSubview(_ view: UIView) {
        let className = "_UIParallaxDimmingView"
        guard let offendingClass = NSClassFromString(className) else { return swizzled_addSubview(view) }
        if (view.isMember(of: offendingClass)) {
            return
        }
        swizzled_addSubview(view)
    }
}
Run Code Online (Sandbox Code Playgroud)

我建议按照以下方式使用它:

class SomeViewController: UIViewController {

    func transition(to viewController: UIViewController) {
        navigationController?.delegate = self
        navigationController?.pushViewController(viewController, animated: true)
    }
}


extension SomeViewController: UINavigationControllerDelegate {
    func navigationController(_ navigationController: UINavigationController, willShow viewController: UIViewController, animated: Bool) {
        UIView.preventDimmingView()
    }

    func navigationController(_ navigationController: UINavigationController, didShow viewController: UIViewController, animated: Bool) {
        UIView.allowDimmingView()
    }
}
Run Code Online (Sandbox Code Playgroud)

当然,如果您希望它通过 App Store 审核,您可能会被标记为"_UIParallaxDimmingView"字符串。我建议改为从字节数组初始化它:

let className = String(bytes: [95, 85, 73, 80, 97, 114, 97, 108, 108, 97, 120, 68, 105, 109, 109, 105, 110, 103, 86, 105, 101, 119], encoding: .utf8)!
Run Code Online (Sandbox Code Playgroud)

gif