带有 tabViewStyle 页面的 SwiftUI TabView 在 iOS 模拟器中运行,但在设备上崩溃
我正在尝试创建一个 SwiftUI iOS 应用程序,其顶层有一个 NavigationLink,还有一个使用 .page 的 TabView 详细视图,这样我就可以在选项卡之间滑动。是的,我知道 TabView 通常位于顶层,带有 NavigationLink 详细信息。
该应用程序在 iOS 模拟器中运行,但在我的 iPhone 上挂起/崩溃。
这是我想要工作的代码:
ContentView.swift
struct ContentView: View {
@State private var selection: String? = nil
var body: some View {
NavigationView {
VStack {
NavigationLink(
destination: DetailView(),
tag: "tag1",
selection: $selection) {
Text("Show Detail View")
.font(.largeTitle .bold())
}
}
}
}
}
DetailView.swift
struct DetailView: View {
@State private var selectedTab = "one"
var body: some View {
ZStack {
// 3 main tabs
TabView(selection: $selectedTab) {
Text("Tab 1 View")
.font(.largeTitle .bold())
.tag("one")
Text("Tab 2 View")
.font(.largeTitle .bold())
.tag("two")
Text("Tab 3 View")
.font(.largeTitle .bold())
.tag("three")
}
// no dots
.tabViewStyle(.page)
// .tabViewStyle(PageTabViewStyle())
// .id(UUID())
VStack {
Text("\(selectedTab)")
Spacer()
}
}
}
}
简化的 DetailView 也会以同样的方式崩溃:
SimplifiedDetailView.swift
struct DetailView: View {
var body: some View {
TabView {
Text("Tab One")
.font(.largeTitle .bold())
.tag("one")
Text("Tab Two")
.font(.largeTitle .bold())
.tag("two")
}
// no dots
.tabViewStyle(.page)
}
当我在iPhone
时,我可以选择 NavigationLink。它变暗然后挂起而不渲染detailView()。很久以后,我收到一个错误对话框,表明该应用程序由于内存问题而终止。
如果我删除 tabViewStyle 修饰符,应用程序将在设备上运行。当然,它默认为传统的选项卡视图,控件位于设备底部。
我在运行 macOS Monterey 12.2.1 (21D62) 的 M1 mac mini 上运行 Xcode 13.2.1 (13C100)。我正在运行 iOS 15.3.1 的 iPhone 8 Plus 上运行。默认情况下,Xcode 的 iOS 部署目标为 iOS 15.2。
有什么建议吗?
I'm trying to create a SwiftUI iOS app with a NavigationLink at the top level, and a TabView detail view, that uses .page, so I can swipe between tabs. And yes, I know the TabView is typically at the top level, with NavigationLink detal.
The app runs in the iOS simulator, but hangs/crashes on my iPhone.
Here is the code I want to work:
ContentView.swift
struct ContentView: View {
@State private var selection: String? = nil
var body: some View {
NavigationView {
VStack {
NavigationLink(
destination: DetailView(),
tag: "tag1",
selection: $selection) {
Text("Show Detail View")
.font(.largeTitle .bold())
}
}
}
}
}
DetailView.swift
struct DetailView: View {
@State private var selectedTab = "one"
var body: some View {
ZStack {
// 3 main tabs
TabView(selection: $selectedTab) {
Text("Tab 1 View")
.font(.largeTitle .bold())
.tag("one")
Text("Tab 2 View")
.font(.largeTitle .bold())
.tag("two")
Text("Tab 3 View")
.font(.largeTitle .bold())
.tag("three")
}
// no dots
.tabViewStyle(.page)
// .tabViewStyle(PageTabViewStyle())
// .id(UUID())
VStack {
Text("\(selectedTab)")
Spacer()
}
}
}
}
A simplified DetailView also crahses in the same way:
Simplified detailView.swift
struct DetailView: View {
var body: some View {
TabView {
Text("Tab One")
.font(.largeTitle .bold())
.tag("one")
Text("Tab Two")
.font(.largeTitle .bold())
.tag("two")
}
// no dots
.tabViewStyle(.page)
}
}
When I run this on my iPhone, I can select the NavigationLink. it dims then hangs without rendering the detailView(). Much later, I get an error dialog, indicating the app was terminated due to a memory issue.
App runs on device if I remove the tabViewStyle modifier. Of course, it defaults to a traditional tab view with controls on the bottom of the device.
I am running Xcode 13.2.1 (13C100) on an M1 mac mini running macOS Monterey 12.2.1 (21D62). I am running on an iPhone 8 Plus running iOS 15.3.1. Xcode, by default, has an iOS Deployment Target of iOS 15.2.
Any suggestions?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用选择器的示例:
Example using a Picker :