Child NavigationView 不支持 iOS 15 上的修饰符
Child NavigationView doesn't honor modifiers on iOS 15
我有一个 child NavigationView
的问题,它不支持修饰符,您可以尝试使用下面的单个代码文件工作应用程序查看:
import SwiftUI
@main
struct NavigationView_IssueApp: App {
@StateObject var viewState = ViewState()
var body: some Scene {
WindowGroup {
ContentView()
.environmentObject(viewState)
}
}
}
struct ContentView: View {
@EnvironmentObject var viewState: ViewState
var body: some View {
ZStack {
Button("Tap Me") {
viewState.showAllCards.toggle()
}
if !viewState.showAllCards {
ChildNavigationView()
}
}
}
}
struct ChildNavigationView: View {
@EnvironmentObject var viewState: ViewState
var body: some View {
NavigationView {
CardDetailView()
.navigationBarTitleDisplayMode(.inline)
}
.navigationViewStyle(StackNavigationViewStyle())
}
}
struct CardDetailView: View {
@EnvironmentObject var viewState: ViewState
var body: some View {
Color.yellow
.toolbar {
ToolbarItem(placement: .navigationBarTrailing) {
Button(action: { viewState.showAllCards.toggle() }) {
Text("Done")
}
}
}
}
}
class ViewState: ObservableObject {
init() {
print("init ViewState")
}
@Published var showAllCards = true
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
.environmentObject(ViewState())
}
}
在 Canvas 预览中看起来不错。
在 iOS 14 设备上看起来不错。
在 iOS 15 设备上不支持修饰符。
有人知道这种奇怪的行为吗?
看起来像一个错误。这里找到了安全的解决方法
struct ChildNavigationView: View {
@EnvironmentObject var viewState: ViewState
@State private var mode = NavigationBarItem.TitleDisplayMode.automatic
var body: some View {
NavigationView {
CardDetailView()
.navigationBarTitleDisplayMode(mode)
}
.navigationViewStyle(StackNavigationViewStyle())
.onAppear {
mode = .inline // << switch mode right after construction
}
}
}
我有一个 child NavigationView
的问题,它不支持修饰符,您可以尝试使用下面的单个代码文件工作应用程序查看:
import SwiftUI
@main
struct NavigationView_IssueApp: App {
@StateObject var viewState = ViewState()
var body: some Scene {
WindowGroup {
ContentView()
.environmentObject(viewState)
}
}
}
struct ContentView: View {
@EnvironmentObject var viewState: ViewState
var body: some View {
ZStack {
Button("Tap Me") {
viewState.showAllCards.toggle()
}
if !viewState.showAllCards {
ChildNavigationView()
}
}
}
}
struct ChildNavigationView: View {
@EnvironmentObject var viewState: ViewState
var body: some View {
NavigationView {
CardDetailView()
.navigationBarTitleDisplayMode(.inline)
}
.navigationViewStyle(StackNavigationViewStyle())
}
}
struct CardDetailView: View {
@EnvironmentObject var viewState: ViewState
var body: some View {
Color.yellow
.toolbar {
ToolbarItem(placement: .navigationBarTrailing) {
Button(action: { viewState.showAllCards.toggle() }) {
Text("Done")
}
}
}
}
}
class ViewState: ObservableObject {
init() {
print("init ViewState")
}
@Published var showAllCards = true
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
.environmentObject(ViewState())
}
}
在 Canvas 预览中看起来不错。
在 iOS 14 设备上看起来不错。
在 iOS 15 设备上不支持修饰符。
有人知道这种奇怪的行为吗?
看起来像一个错误。这里找到了安全的解决方法
struct ChildNavigationView: View {
@EnvironmentObject var viewState: ViewState
@State private var mode = NavigationBarItem.TitleDisplayMode.automatic
var body: some View {
NavigationView {
CardDetailView()
.navigationBarTitleDisplayMode(mode)
}
.navigationViewStyle(StackNavigationViewStyle())
.onAppear {
mode = .inline // << switch mode right after construction
}
}
}