如何更改 NavigationView 上后退按钮的颜色

Mic*_*air 17 back-button swiftui

当您单击该按钮时,它会将您带到一个新视图并在左上角放置一个后退按钮。我不知道是什么属性控制了后退按钮的颜色。我尝试添加一个重音颜色和前景颜色,但它们只编辑视图内的项目。

var body: some View {
    NavigationView {
        NavigationLink(destination: ResetPasswordView()) {
            Text("Reset Password")
            .foregroundColor(Color(red: 0, green: 116 / 255, blue: 217 / 255))
            .padding()
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

tur*_*ted 45

您可以使用accentColorNavigationView 上的属性来设置后退按钮颜色,如下例所示:

var body: some View {
    NavigationView {
        List(1..<13) { item in
            NavigationLink(destination: Text("\(item) x 8 = \(item*8)")) {
                Text(String(item))
            }
        }.navigationBarTitle("Table of 8")
    }.accentColor( .black) // <- note that it's added here and not on the List like navigationBarTitle()
}
Run Code Online (Sandbox Code Playgroud)

  • `accentColor` 现在被标记为即将弃用,并建议“使用资产目录的强调色或 View.tint(_:) 代替。”。我尝试过使用“tint”,但这只对我通过“toolbar()”修饰符添加的附加按钮进行着色。 (7认同)

mgy*_*yky 13

资产目录解决方案

\n

在资产目录 (Assets.xcassets) 中添加AccentColor设置。Xcode 应用您在此颜色集中指定的颜色作为您的 app\xe2\x80\x99s 强调色。

\n

简短的解决方案

\n

将强调色添加到资产目录后,导航栏后退按钮将变为该颜色。这就是您所需要的。

\n

更多细节

\n

如果您的应用没有\xe2\x80\x99t 有 AccentColor 颜色集,请通过下面列出的步骤手动创建颜色集:

\n
    \n
  1. 在项目导航器中,选择资产目录。
  2. \n
  3. 单击大纲视图底部的添加按钮 (+)。
  4. \n
  5. 在弹出菜单中,选择颜色设置。新的颜色集出现在大纲视图中并在详细信息区域中打开。
  6. \n
  7. 双击大纲视图中的颜色集名称,以使用描述性名称重命名颜色集,然后按 Return 键。
  8. \n
  9. 在“构建设置”中,找到 \xe2\x80\x9cGlobal Accent Color Name\xe2\x80\x9d 的构建设置。双击构建设置,输入强调颜色集的名称,然后按 Return 键。
  10. \n
\n

从代码中访问强调色

\n

默认情况下,强调色适用于应用程序中使用色调颜色的所有视图和控件,除非您覆盖视图层次结构的特定子集的颜色。但是,您可能希望将强调色合并到用户界面中不\xe2\x80\x99 不依赖色调颜色的其他部分,例如静态文本元素。\n要在代码中使用资产目录中的强调色值,像这样加载颜色:

\n

斯威夫特用户界面

\n
Text("Accent Color")\n    .foregroundStyle(Color.accentColor)\n
Run Code Online (Sandbox Code Playgroud)\n

用户界面工具包

\n
label.textColor = UIColor.tintColor\n
Run Code Online (Sandbox Code Playgroud)\n

什么是强调色?

\n

强调色或色调颜色是一种广泛的主题颜色,适用于应用程序中的视图和控件。使用强调色快速为您的应用创建统一的配色方案。您可以通过在资产目录中指定强调色来为应用程序设置强调色。

\n

强调色

\n

有关该主题的更多信息请查看文档

\n


小智 8

我怀疑这是正确的方法,但我通过修改 SceneDelegate.swift 来设置窗口色调颜色来让它工作。

func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {

    // Use a UIHostingController as window root view controller
    let window = UIWindow(frame: UIScreen.main.bounds)
    window.rootViewController = UIHostingController(rootView: ContentView())
    window.tintColor = .green // set the colour of the back navigation text
    self.window = window
    window.makeKeyAndVisible()
}
Run Code Online (Sandbox Code Playgroud)


pan*_*kaj 8

将 .accentColor 修饰符添加到 NavigationView ,如下所示

NavigationView {
    Text("Hello World")
      .toolbar {
                    ToolbarItem(placement: .navigationBarLeading) {
                        Button("Close") { }
                    }
            }
 }
 .accentColor(.themeColor)
Run Code Online (Sandbox Code Playgroud)


Ell*_*ogo 6

如果您使用的是 XCode 12,请考虑Assets.xcassets. XCode 将为应用程序中的所有导航后退按钮使用此颜色。

请注意,这也会影响其他 UI 元素。其中之一是按钮。您始终可以使用accentColor修饰符来覆盖它:

Button("Button With Different Accent Color") { 
   // do something useful
}
.accentColor(.red)
Run Code Online (Sandbox Code Playgroud)


Pre*_*zic 5

我试图做同样的事情有一段时间了,但认为还没有 SwiftUI 解决方案。可以完成工作的一件事(如果它适用于您的用例)是 UIKit 的外观:

UINavigationBar.appearance().tintColor = .black

  • 从 iOS 16 开始,此解决方案不再有效。 (7认同)