swift - 推动视图控制器时快速背景颜色
解决方案
Using a UIViewControllerAnimatedTransitioning
would definitely be the "correct" way, but it seems you really want to not use it.
If you want to "hack" it, then swizzling is the way to go.
Here is an extension on UIView
that prevents the underlying class _UIParallaxDimmingView
from being displayed.
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)
}
}
I would recommend using it along the lines of this:
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()
}
}
Of course if you want this to make it through App Store review, you will likely get flagged for the "_UIParallaxDimmingView"
string. I would recommend initializing it from a byte array instead:
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)!
推荐阅读
- r - 有没有一种简单的方法来比较列表中数据框的匹配元素并替换到数据框中?
- asp.net-core - .NET Core 3.1 的 Azure DevOps 代码覆盖率
- reporting-services - SSRS - 每页显示矩阵两次
- php - 通过php添加并显示自定义标题小部件区域
- regex - grep 命令,寻找首字母大写,没有数字和一些原生字符
- php - Prestashop - 对模块内的自定义 PHP 文件的 GET 请求返回 404
- mongodb - 如何在 mongo 中查找不包含数组元素的文档
- forms - 带有表单的 Google Apps 脚本:提交后发布标记和反馈
- reactjs - 在创建或使用 React 应用程序时,我是否需要始终连接到互联网?
- javascript - 在Javascript中添加未定义数组的总和