弹出视图控制器时更改导航栏颜色

时间:2017-03-06 06:21:23

标签: ios swift uinavigationbar

我有三个视图控制器。在第一个视图控制器(FirstVC)中,导航栏的条形色调为clearColor,条形本身是半透明的。当我点击某些内容时,我会推送到导航栏需要不透明的SecondVC。所以我将barTintColor设置为某个颜色值,并将isTranslucent设置为false。当我从SecondVC推送到ThirdVC时,导航栏又需要是半透明的。当我弹出ThirdVC并返回SecondVC时会出现问题。导航栏在一秒钟内显示为透明,然后根据需要变为不透明。我无法理解延误的原因。

从SecondVC的viewWillDisappear()调用以下方法。我试过从fileprivate func configureNavigationBar(){ self.navigationController?.navigationBar.setBackgroundImage(nil, for: UIBarMetrics.default) self.navigationController?.navigationBar.shadowImage = nil self.navigationController?.navigationBar.isTranslucent = false self.navigationController?.navigationBar.barTintColor = Style.Movie.primaryBackgroundColor let titleDict: NSDictionary = [NSForegroundColorAttributeName: UIColor.white] self.navigationController?.navigationBar.titleTextAttributes = titleDict as? [String:Any] } 的ThirdVC做同样的事情,但没有效果。

public double aResult(Calculator other)
{
    other = new Calculator(getNumber());
    other.whatOperator();
    this.result = result;
    return result;

} 

如果我向后滑动而不是点击后退按钮,它可以正常工作。

5 个答案:

答案 0 :(得分:38)

在thiredVC中使用此功能

class

答案 1 :(得分:2)

在此线程中使用,并提出了一个很好的解决方案来将来为所有人提供帮助。

首先创建一个类型为UINavigationController的自定义enum,这将有助于定义您的导航设置:

enum NavType: Int {
    case light, medium, dark
}

class NavigationController: UINavigationController {

    /* 
      Fetch the last controller in the navigation stack so the 
      ViewControllers can switch the navType
    */
    var previousController: ViewController? {
        if viewControllers.count > 1 {
            return viewControllers[viewControllers.count-2] as? ViewController
        }
        return nil
    }

    var navType: NavType = .light {
        didSet { updateNavType() }
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        navigationBar.isTranslucent = false
        layoutNavigationTheme()
    }

    func layoutNavigationTheme() {
        switch navType {
        case .dark:
            view.backgroundColor = .white
            navigationBar.backgroundColor = .black
            navigationBar.barTintColor = .black
            navigationBar.tintColor = .white

        ... // Set up as per enum
        }
    }

}

然后使用UIViewController创建自定义willMove(...

class ViewController: UIViewController {

    var navType: NavType = .light

    var navigation: NavigationController? {
        return navigationController as? NavigationController
    }

    // Override willMove will fetch the last controller and set the navType
    override func willMove(toParentViewController parent: UIViewController?) {
        super.willMove(toParentViewController: parent)

        if let navigation = navigation {
            navigation.navType = navigation.previousController?.navType ?? .light
        }
    }

}

然后只需在UIViewControllers中将新的ViewController子类化,并在navType中设置viewDidLoad

class MainController: ViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        navType = .dark
    }

}

答案 2 :(得分:0)

您可以为SecondVC制作自定义导航栏。并在后退按钮上手动调用-popViewController方法。

答案 3 :(得分:0)

另一个可行的解决方案是重写pushViewController(_ viewController:UIViewController,动画:Bool)和popViewController(animated:Bool)-> UIViewController? UINavigationController。

class CustomNC : UINavigationController {
    public override init(rootViewController: UIViewController) {
        super.init(rootViewController: rootViewController)
    }

    public override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: Bundle?) {
        super.init(nibName: nil, bundle: nil)
    }

    public required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    private var isCustomDesign: Bool { 
        return viewControllers.count == 1 && viewControllers[0] is MyCustomVC // Or any other condition 
    }

    override func pushViewController(_ viewController: UIViewController, animated: Bool) {
        super.pushViewController(viewController, animated: animated)
        if isCustomDesign {
            navigationBar.barTintColor = UIColor.red
        } else {
            navigationBar.barTintColor = UIColor.green
        }
    }

    override func popViewController(animated: Bool) -> UIViewController? {
        let viewController = super.popViewController(animated: animated)
        if isCustomDesign {
            navigationBar.barTintColor = UIColor.red
        } else {
            navigationBar.barTintColor = UIColor.green
        }
        return viewController
    }
}

答案 4 :(得分:0)

override func viewWillAppear(_ animated: Bool)

您可以在执行 top viewcontroller pop 操作时调用的函数中设置您的视图属性。