NavigationBar渐变动画的实现

1, 页面加载完毕即将显示时, 保存当前NavigationBar及其分割线的背景图片, 并分别设置其背景图片为新创建空白图片

overridefuncviewWillAppear(_animated:Bool) {

        super.viewWillAppear(animated)

        naviBarBgImage = navigationController?.navigationBar.backgroundImage(for: .default)

        navigationController?.navigationBar.setBackgroundImage(UIImage(), for: .default)

        naviBarBgShadowImage = navigationController?.navigationBar.shadowImage

        navigationController?.navigationBar.shadowImage = UIImage()

    }

2, 在页面即将消失时, 还原其背景图片

overridefuncviewWillDisappear(_animated:Bool) {

        super.viewWillDisappear(animated)

        navigationController?.navigationBar.setBackgroundImage(naviBarBgImage, for: .default)

        navigationController?.navigationBar.shadowImage = naviBarBgShadowImage

    }

你可能感兴趣的:(NavigationBar渐变动画的实现)