SwiftUI:出现弹出视图时如何更改内联导航栏颜色?
SwiftUI: How can I change inline NavigationBar color when PopupView appears?
我想在 PopupView 出现时更改内联 NavigationBar 颜色。
struct TestView3: View {
@State private var showPopup: Bool = true
var body: some View {
NavigationView {
ZStack {
ScrollView {
ForEach(0..<100) { i in
Text("text \(i)")
}
}
BackgroundOverlayView {
BasePopupView(titleText: nil, primaryText: "dd", secondaryText: nil, primaryButtonTitle: "aa", primaryButtonAction: {
self.showPopup = false
}, secondaryButtonTitle: nil, secondaryButtonAction: nil)
}
.hidden(!showPopup)
}
.navigationBarTitle("title", displayMode: .inline)
}
}
}
这不是我想要的。 ->
https://i.stack.imgur.com/Fq5S1.png
这就是我想做的。 - >
https://i.stack.imgur.com/T2j2W.png
所以,我添加了以下代码,它按我想要的方式显示,但有一个问题,因为 NavigationBar 一起清晰。问题:https://i.stack.imgur.com/4oToY.png
init() {
UINavigationBar.appearance().setBackgroundImage(UIImage(), for: .default)
}
struct TestView3: View {
@State private var showPopup: Bool = true
init() {
UINavigationBar.appearance().setBackgroundImage(UIImage(), for: .default)
}
var body: some View {
NavigationView {
ZStack {
ScrollView {
ForEach(0..<100) { i in
Text("text \(i)")
}
}
BackgroundOverlayView {
BasePopupView(titleText: nil, primaryText: "dd", secondaryText: nil, primaryButtonTitle: "aa", primaryButtonAction: {
self.showPopup = false
}, secondaryButtonTitle: nil, secondaryButtonAction: nil)
}
.hidden(!showPopup)
}
.navigationBarTitle("title", displayMode: .inline)
}
}
}
struct BackgroundOverlayView<Content: View>: View {
let content: () -> Content
init(@ViewBuilder content: @escaping () -> Content) {
self.content = content
}
var body: some View {
VStack {
Spacer()
content()
Spacer()
}
.background(Color(white: 0/255, opacity: 0.2))
.edgesIgnoringSafeArea(.all)
}
}
如何在popupview出现的时候一起改变导航栏的颜色覆盖而不是完全清晰?
谢谢
您不能只更改一个视图中的一个特定导航栏,但是您可以 hacky/trick 做一些事情:
您已经添加了这些行:
UINavigationBar.appearance().setBackgroundImage(UIImage(), for: .default)
UINavigationBar.appearance().shadowImage = UIImage()
并且您提到导航栏完全清晰,并且可以通过导航栏 is/should 所在的位置看到您的内容,要解决此问题,只需向您的 ScrollView
添加填充,如下所示:
ScrollView {
ForEach(0..<100) { i in
Text("text \(i)")
}
}.padding(.top)
这会阻止您的内容“通过”您的导航栏。
我想在 PopupView 出现时更改内联 NavigationBar 颜色。
struct TestView3: View {
@State private var showPopup: Bool = true
var body: some View {
NavigationView {
ZStack {
ScrollView {
ForEach(0..<100) { i in
Text("text \(i)")
}
}
BackgroundOverlayView {
BasePopupView(titleText: nil, primaryText: "dd", secondaryText: nil, primaryButtonTitle: "aa", primaryButtonAction: {
self.showPopup = false
}, secondaryButtonTitle: nil, secondaryButtonAction: nil)
}
.hidden(!showPopup)
}
.navigationBarTitle("title", displayMode: .inline)
}
}
}
这不是我想要的。 -> https://i.stack.imgur.com/Fq5S1.png
这就是我想做的。 - > https://i.stack.imgur.com/T2j2W.png
所以,我添加了以下代码,它按我想要的方式显示,但有一个问题,因为 NavigationBar 一起清晰。问题:https://i.stack.imgur.com/4oToY.png
init() {
UINavigationBar.appearance().setBackgroundImage(UIImage(), for: .default)
}
struct TestView3: View {
@State private var showPopup: Bool = true
init() {
UINavigationBar.appearance().setBackgroundImage(UIImage(), for: .default)
}
var body: some View {
NavigationView {
ZStack {
ScrollView {
ForEach(0..<100) { i in
Text("text \(i)")
}
}
BackgroundOverlayView {
BasePopupView(titleText: nil, primaryText: "dd", secondaryText: nil, primaryButtonTitle: "aa", primaryButtonAction: {
self.showPopup = false
}, secondaryButtonTitle: nil, secondaryButtonAction: nil)
}
.hidden(!showPopup)
}
.navigationBarTitle("title", displayMode: .inline)
}
}
}
struct BackgroundOverlayView<Content: View>: View {
let content: () -> Content
init(@ViewBuilder content: @escaping () -> Content) {
self.content = content
}
var body: some View {
VStack {
Spacer()
content()
Spacer()
}
.background(Color(white: 0/255, opacity: 0.2))
.edgesIgnoringSafeArea(.all)
}
}
如何在popupview出现的时候一起改变导航栏的颜色覆盖而不是完全清晰? 谢谢
您不能只更改一个视图中的一个特定导航栏,但是您可以 hacky/trick 做一些事情:
您已经添加了这些行:
UINavigationBar.appearance().setBackgroundImage(UIImage(), for: .default)
UINavigationBar.appearance().shadowImage = UIImage()
并且您提到导航栏完全清晰,并且可以通过导航栏 is/should 所在的位置看到您的内容,要解决此问题,只需向您的 ScrollView
添加填充,如下所示:
ScrollView {
ForEach(0..<100) { i in
Text("text \(i)")
}
}.padding(.top)
这会阻止您的内容“通过”您的导航栏。