gpt4 book ai didi

SwiftUI 在详细 View 中覆盖导航栏外观

转载 作者:行者123 更新时间:2023-12-03 16:11:15 24 4
gpt4 key购买 nike

我有一个 super 简单的 SwiftUI 主从应用程序:

import SwiftUI

struct ContentView: View {
@State private var imageNames = [String]()

var body: some View {
NavigationView {
MasterView(imageNames: $imageNames)
.navigationBarTitle(Text("Master"))
.navigationBarItems(
leading: EditButton(),
trailing: Button(
action: {
withAnimation {
// simplified for example
self.imageNames.insert("image", at: 0)
}
}
) {
Image(systemName: "plus")
}
)
}
}
}

struct MasterView: View {
@Binding var imageNames: [String]

var body: some View {
List {
ForEach(imageNames, id: \.self) { imageName in
NavigationLink(
destination: DetailView(selectedImageName: imageName)
) {
Text(imageName)
}
}
}
}
}

struct DetailView: View {

var selectedImageName: String

var body: some View {
Image(selectedImageName)
}
}


我还在 SceneDelegate 上为导航栏的颜色设置外观代理”
    func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
// Use this method to optionally configure and attach the UIWindow `window` to the provided UIWindowScene `scene`.
// If using a storyboard, the `window` property will automatically be initialized and attached to the scene.
// This delegate does not imply the connecting scene or session are new (see `application:configurationForConnectingSceneSession` instead).

let navBarAppearance = UINavigationBarAppearance()
navBarAppearance.configureWithOpaqueBackground()
navBarAppearance.shadowColor = UIColor.systemYellow
navBarAppearance.backgroundColor = UIColor.systemYellow
navBarAppearance.shadowImage = UIImage()
UINavigationBar.appearance().standardAppearance = navBarAppearance
UINavigationBar.appearance().scrollEdgeAppearance = navBarAppearance

// Create the SwiftUI view that provides the window contents.
let contentView = ContentView()

// Use a UIHostingController as window root view controller.
if let windowScene = scene as? UIWindowScene {
let window = UIWindow(windowScene: windowScene)
window.rootViewController = UIHostingController(rootView: contentView)
self.window = window
window.makeKeyAndVisible()
}
}

现在,我想要做的是在详细 View 出现时将导航栏的背景颜色更改为清除。我仍然想要那个 View 中的后退按钮,所以隐藏导航栏并不是一个理想的解决方案。我还希望更改仅适用于 Detail View ,因此当我弹出该 View 时,外观代理应该接管,如果我推送到另一个 Controller ,则外观代理也应该接管。

我一直在尝试各种各样的事情:
- 在 didAppear 上更改外观代理
- 将详细 View 包装在 UIViewControllerRepresentable 中(成功有限,我可以进入导航栏并更改其颜色,但由于某种原因,导航 Controller 不止一个)

在 SwiftUI 中是否有一种直接的方法可以做到这一点?

最佳答案

我更喜欢为此使用 ViewModifer。下面是我的自定义 ViewModifier

struct NavigationBarModifier: ViewModifier {

var backgroundColor: UIColor?

init(backgroundColor: UIColor?) {
self.backgroundColor = backgroundColor

let coloredAppearance = UINavigationBarAppearance()
coloredAppearance.configureWithTransparentBackground()
coloredAppearance.backgroundColor = backgroundColor
coloredAppearance.titleTextAttributes = [.foregroundColor: UIColor.white]
coloredAppearance.largeTitleTextAttributes = [.foregroundColor: UIColor.white]

UINavigationBar.appearance().standardAppearance = coloredAppearance
UINavigationBar.appearance().compactAppearance = coloredAppearance
UINavigationBar.appearance().scrollEdgeAppearance = coloredAppearance
UINavigationBar.appearance().tintColor = .white
}

func body(content: Content) -> some View {
ZStack{
content
VStack {
GeometryReader { geometry in
Color(self.backgroundColor ?? .clear)
.frame(height: geometry.safeAreaInsets.top)
.edgesIgnoringSafeArea(.top)
Spacer()
}
}
}
}}

您还可以使用不同的文本颜色和色调来初始化它,我现在已经添加了静态颜色。

您可以从任何调用此修饰符。在你的情况下
    NavigationLink(
destination: DetailView(selectedImageName: imageName)
.modifier(NavigationBarModifier(backgroundColor: .green))

)

下面是截图。
Detail View with green navigation bar

关于SwiftUI 在详细 View 中覆盖导航栏外观,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62026578/

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