如何更改导航栏中的背景或颜色?

Alb*_*nas 4 navigationbar swiftui

目前我找不到任何方法来更改 SwiftUI 中导航栏的颜色/背景。有小费吗?

Max*_*ius 5

为了更改所有视图控制器的导航栏颜色,您必须在AppDelegate.swift文件中设置它

添加以下代码以didFinishLaunchingWithOptions使其起作用AppDelegate.swift

var navigationBarAppearace = UINavigationBar.appearance()

navigationBarAppearace.tintColor = uicolorFromHex(0xffffff)
navigationBarAppearace.barTintColor = uicolorFromHex(0x034517)
Run Code Online (Sandbox Code Playgroud)

在此tintColor属性中更改导航栏的背景颜色。

barTintColor属性对颜色的影响:

  • 后退指示图像
  • 按钮标题
  • 按钮 图片

奖金:

更改导航栏标题的颜色:

// change navigation item title color
navigationBarAppearace.titleTextAttributes =[NSForegroundColorAttributeName:UIColor.whiteColor()]
Run Code Online (Sandbox Code Playgroud)

titleTextAttributes影响标题文本

我希望它有帮助。:)