gpt4 book ai didi

ios - 如何更改与初始颜色不同的导航栏按钮项目颜色?

转载 作者:可可西里 更新时间:2023-11-01 01:48:33 27 4
gpt4 key购买 nike

我已经从 View A 导航到 View B(使用新的导航 Controller 和黑色导航栏)

查看一个 Controller :

    // With Navigation Controller
let storyBoard: UIStoryboard = UIStoryboard(name: "ViewB", bundle: nil)
let newViewController = storyBoard.instantiateViewController(withIdentifier: "ViewB") as! ViewBController
let navCont = UINavigationController(rootViewController: newViewController)
// Change the navigation bar to translucent
navCont.navigationBar.setBackgroundImage(UIImage(), for: .default)
navCont.navigationBar.shadowImage = UIImage()
navCont.navigationBar.isTranslucent = true
navCont.navigationBar.tintColor = UIColor.black
//present(navCont, animated: true, completion: nil)
show(navCont, sender: nil)

当从 View B 导航到 View C 时,我想将 navigationBar.tintColor 从黑色更改为白色。

View B Controller :

@IBAction func staticQRBtnPressed(_ sender: Any) {
// Without Navigation Controller
let storyBoard: UIStoryboard = UIStoryboard(name: "ViewC", bundle: nil)
let newViewController = storyBoard.instantiateViewController(withIdentifier: "ViewCController") as! ViewCController
newViewController.navigationController?.navigationBar.barTintColor = UIColor.white
newViewController.navigationController?.navigationBar.titleTextAttributes = [.foregroundColor: UIColor.white]
self.show(newViewController, sender: nil) // Push to navigation stack
}

查看 C Controller :

override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
navigationController?.navigationBar.barTintColor = UIColor.white
navigationController?.navigationBar.titleTextAttributes = [.foregroundColor: UIColor.white]
}

为什么上面的方法都不行?

最佳答案

除了为什么它不起作用,它不是交互式的。因此,如果用户向后滑动且未与导航动画同步,它将无法正常工作。因此,如果您想要一个有效的交互式解决方案,我们开始吧:

定义自定义颜色协议(protocol):

/// Navigation bar colors for `ColorableNavigationController`, called on `push` & `pop` actions
public protocol NavigationBarColorable: class {
var navigationTintColor: UIColor? { get }
var navigationBarTintColor: UIColor? { get }
}

public extension NavigationBarColorable {
var navigationTintColor: UIColor? { return nil }
}

子类 UINavigationController 并覆盖导航方法:

/**
UINavigationController with different colors support of UINavigationBar.
To use it please adopt needed child view controllers to protocol `NavigationBarColorable`.
- note: Don't forget to set initial tint and barTint colors
*/
class AppNavigationController: UINavigationController {

override func viewDidLoad() {
super.viewDidLoad()
navigationBar.setBackgroundImage(UIImage(), for: .any, barMetrics: .default)
navigationBar.shadowImage = UIImage()
if let colors = rootViewController as? NavigationBarColorable {
setNavigationBarColors(colors)
}
}

private var previousViewController: UIViewController? {
guard viewControllers.count > 1 else {
return nil
}
return viewControllers[viewControllers.count - 2]
}

override open func pushViewController(_ viewController: UIViewController, animated: Bool) {
if let colors = viewController as? NavigationBarColorable {
setNavigationBarColors(colors)
}

setTabBarHidden(viewController is TabBarHidable)

super.pushViewController(viewController, animated: animated)
}

override open func popViewController(animated: Bool) -> UIViewController? {
if let colors = previousViewController as? NavigationBarColorable {
setNavigationBarColors(colors)
}

setTabBarHidden(previousViewController is TabBarHidable)

// Let's start pop action or we can't get transitionCoordinator()
let popViewController = super.popViewController(animated: animated)

// Secure situation if user cancelled transition
transitionCoordinator?.animate(alongsideTransition: nil, completion: { [weak self] context in
guard let `self` = self else { return }
self.setTabBarHidden(self.topViewController is TabBarHidable)
guard let colors = self.topViewController as? NavigationBarColorable else { return }
self.setNavigationBarColors(colors)
})

return popViewController
}

override func popToRootViewController(animated: Bool) -> [UIViewController]? {
if let colors = rootViewController as? NavigationBarColorable {
setNavigationBarColors(colors)
}

let controllers = super.popToRootViewController(animated: animated)

return controllers
}

private func setNavigationBarColors(_ colors: NavigationBarColorable) {

if let tintColor = colors.navigationTintColor {
navigationBar.titleTextAttributes = [
.foregroundColor : tintColor,
.font : R.font.iranSansFaNumBold(size: 14)!
]
navigationBar.tintColor = tintColor
}

navigationBar.barTintColor = colors.navigationBarTintColor
}
}

并在每个你想要自定义导航颜色的 View Controller 中遵循和实现协议(protocol)方法:

extension MyCustomViewController: NavigationBarColorable {
public var navigationBarTintColor: UIColor? { return .red }
public var navigationTintColor: UIColor? { return .blue }
}

注意 1:我添加了文本颜色更改支持。

注意 2:我使用了我的一个项目代码作为这个答案的基础,如果您看到一些非通用的命名等,我们深表歉意。

注意 3:正如我在代码中提到的,不要忘记设置初始色调和 barTint 颜色。

关于ios - 如何更改与初始颜色不同的导航栏按钮项目颜色?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56732556/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com