首页 > 解决方案 > UINavigationController 过渡到 defaultBackground 显示白色边框

问题描述

所以我想实现从模糊到透明背景的干净导航栏过渡,反之亦然。这段代码完成了它的工作,转换有点小故障。仅当您使用滑动手势转到上一个视图控制器时才会发生这种情况。过渡到应有的透明背景外观,但过渡到模糊会伴随视觉故障。

我尝试将updateToBlurry()移动到RedViewController 的viewWillAppear并且我也尝试不使用外观并将 navigationBar 的 backgroundImage 设置为 UIImage() 或 nil,但它做同样的事情。它在没有缺口的 iPhone 上更为明显,但在有缺口的 iPhone 上仍然几乎看不到。我观察到它只有在嵌入 UITabBarController 时才会出现故障。

故事板设置和视频: https ://imgur.com/a/W5gxdwd

这是代码:

final class NavigationController: UINavigationController {
    
    override func viewDidLoad() {
        super.viewDidLoad()
        navigationBar.scrollEdgeAppearance = getTransparentAppearance()
        navigationBar.standardAppearance = getBlurryAppearance()
        navigationBar.compactAppearance = getBlurryAppearance()
    }
    
    func updateToTransparent() {
        navigationBar.standardAppearance = getTransparentAppearance()
        navigationBar.compactAppearance = getTransparentAppearance()
    }
    
    func updateToBlurry() {
        navigationBar.standardAppearance = getBlurryAppearance()
        navigationBar.compactAppearance = getBlurryAppearance()
    }
    
    private func getTransparentAppearance() -> UINavigationBarAppearance {
        let appearance = UINavigationBarAppearance()
        appearance.configureWithTransparentBackground()
        return appearance
    }
    
    private func getBlurryAppearance() -> UINavigationBarAppearance {
        let appearance = UINavigationBarAppearance()
        appearance.configureWithDefaultBackground()
        return appearance
    }
    
}
final class BlueViewController: UIViewController {
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        if let navigationController = navigationController as? NavigationController {
            navigationController.updateToTransparent()
        }
    }

    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)
        if let navigationController = navigationController as? NavigationController {
            navigationController.updateToBlurry()
        }
    }
    
}

标签: iosswiftuinavigationcontrolleruitabbarcontrollervisual-glitch

解决方案


推荐阅读