在 Xcode11Beta (11M336w) 中使用 SwiftUI 的 TabbedView [英] TabbedView using SwiftUI in Xcode11Beta (11M336w)

查看:34
本文介绍了在 Xcode11Beta (11M336w) 中使用 SwiftUI 的 TabbedView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在关注 WWDC2019 的会议:

然而,在WWDC2019 session中,使用了以下代码:

//第2节|ContentView (Apple 的)---------------------------

导入 SwiftUI结构内容视图:查看{var主体:一些视图{导航视图{TabbedView(选择:.constant(1)){PlaceForm().tabItemLabel {图像(系统名称:square.and.pencil")文本(Tab1")}收藏夹表单().tabItemLabel {图像(系统名称:clock.fill")文本(Tab2")}}}}}#if 调试struct ContentView_Previews : PreviewProvider {静态 var 预览:一些视图 {内容视图()}}#万一

//---------------------------

但是,在 Xcode11Beta 上,这会导致 Xcode11Beta 抛出以下编译器错误

无法转换类型为TabbedView,_ModifiedContent<FavoritesForm, _TraitWritingModifier<AnyView?>>)>>'到关闭结果类型'_'

如下面的截图所示

//---------------------------

如果 WWDC2019 演示文稿中的信息正确,那么 WWDC2019 幻灯片中演示的代码不会导致图像显示在选项卡式视图的选项卡中的原因是什么?

此外,使用第 1 节中的代码,将选项卡切换到选项卡 2 会显示一个空白视图,如以下问题所述:

SwiftUI TabbedView 只显示第一个标签的内容

请注意PlaceForm和FavoritesForm的内容如下

//第3节|PlaceForm---------------------------

导入 SwiftUIstruct PlaceForm : 查看{var主体:一些视图{列表 {虚拟堆栈{地图视图().edgesIgnoringSafeArea(.top).frame(高度:300)圆图像().offset(y: -130).padding(.bottom, -130)虚拟堆栈{虚拟堆栈{文本(龟岩").font(.title).颜色:黑色)}堆栈{文本(约书亚树国家公园").font(.subheadline)垫片()文本(加利福尼亚").font(.subheadline)}}.填充()}}.listStyle(.grouped)}}#if 调试struct PlaceForm_Previews : PreviewProvider {静态 var 预览:一些视图 {地表()}}#万一

//第4节|收藏表格---------------------------

导入 SwiftUI结构收藏夹形式:查看{var主体:一些视图{列表 {虚拟堆栈{地图视图().edgesIgnoringSafeArea(.top).frame(高度:300)圆图像().offset(y: -130).padding(.bottom, -130)虚拟堆栈{虚拟堆栈{文本(忍者摇滚").font(.title).颜色:黑色)}堆栈{文本(约书亚树国家公园").font(.subheadline)垫片()文本(加利福尼亚").font(.subheadline)}}.填充()}}.listStyle(.grouped)}}#if 调试struct FavoritesForm_Previews : PreviewProvider {静态 var 预览:一些视图 {收藏表格()}}#万一

解决方案

此问题已在 Xcode 11 beta 3 中修复.来自 iOS &iPadOS 13 Beta 3 发行说明:

<块引用>

tabItemLabel(:) 修饰符 - 现在命名为 tabItem(:) - 现在接受@ViewBuilder 关闭.(51502668)

示例:

myView().tabItem {图像(系统名称:圆圈")文本(Tab1")}

I am following along with the session from WWDC2019 here :

https://developer.apple.com/videos/play/wwdc2019/216/

I have the following code working for creating a TabbedView using SwiftUI :

//Section1 | ContentView (mine)---------------------------

import SwiftUI

struct ContentView : View {
    var body: some View {
        NavigationView {
            TabbedView(selection: .constant(1)) {
                PlaceForm().tabItemLabel(Text("Tab1")).tag(1)
                FavoritesForm().tabItemLabel(Text("Tab2")).tag(2)
            }
        }

    }
}

#if DEBUG
struct ContentView_Previews : PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}
#endif

//--------------------------- The above produces the following tabbed view :

However, in the WWDC2019 session, the following code is used :

//Section2 | ContentView (Apple's)---------------------------

import SwiftUI

struct ContentView : View {
    var body: some View {
        NavigationView {
            TabbedView(selection: .constant(1)) {
                PlaceForm().tabItemLabel {
                    Image(systemName: "square.and.pencil")
                    Text("Tab1")
                }
                FavoritesForm().tabItemLabel {
                    Image(systemName: "clock.fill")
                    Text("Tab2")
                }
            }
        }

    }
}

#if DEBUG
struct ContentView_Previews : PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}
#endif

//---------------------------

However, on Xcode11Beta, this results in the following compiler error being thrown by Xcode11Beta

Cannot convert value of type 'TabbedView<Int, 

TupleView<(_ModifiedContent<PlaceForm, _TraitWritingModifier<AnyView?>>, 

_ModifiedContent<FavoritesForm, _TraitWritingModifier<AnyView?>>)>>' to 

closure result type '_'

as seen in the following screenshots

and

//---------------------------

What is the reason that the code demonstrated in the WWDC2019 slides don't result in the images showing up in the tabs of the tabbed view as should be expected if the information in the WWDC2019 presentation is correct?

Also, with the code in section1, switching tabs to tab2 shows a blank view as described in the following question :

SwiftUI TabbedView only shows first tab's content

Please note that the contents of PlaceForm and FavoritesForm are as reproduced below

//Section3 | PlaceForm---------------------------

import SwiftUI

struct PlaceForm : View {
    var body: some View {
        List {
            VStack {
                MapView()
                    .edgesIgnoringSafeArea(.top)
                    .frame(height: 300)

                CircleImage()
                    .offset(y: -130)
                    .padding(.bottom, -130)
                VStack {
                    VStack {
                        Text("Turtle Rock")
                            .font(.title)
                            .color(.black)
                    }
                    HStack {
                        Text("Joshua Tree National Park")
                            .font(.subheadline)
                        Spacer()
                        Text("California")
                            .font(.subheadline)
                    }
                    }
                    .padding()
            }
        }.listStyle(.grouped)

    }
}

#if DEBUG
struct PlaceForm_Previews : PreviewProvider {
    static var previews: some View {
        PlaceForm()
    }
}
#endif

//Section4 | FavoritesForm---------------------------

import SwiftUI

struct FavoritesForm : View {
    var body: some View {
        List {
            VStack {
                MapView()
                    .edgesIgnoringSafeArea(.top)
                    .frame(height: 300)

                CircleImage()
                    .offset(y: -130)
                    .padding(.bottom, -130)
                VStack {
                    VStack {
                        Text("Ninja Rock")
                            .font(.title)
                            .color(.black)
                    }
                    HStack {
                        Text("Joshua Tree National Park")
                            .font(.subheadline)
                        Spacer()
                        Text("California")
                            .font(.subheadline)
                    }
                    }
                    .padding()
            }
            }.listStyle(.grouped)

    }
}

#if DEBUG
struct FavoritesForm_Previews : PreviewProvider {
    static var previews: some View {
        FavoritesForm()
    }
}
#endif

解决方案

This issue was fixed with Xcode 11 beta 3. From iOS & iPadOS 13 Beta 3 Release Notes:

The tabItemLabel(:) modifier — now named tabItem(:) — now accepts @ViewBuilder closures. (51502668)

Example:

myView()
    .tabItem {
        Image(systemName: "circle")
        Text("Tab1")
    }

这篇关于在 Xcode11Beta (11M336w) 中使用 SwiftUI 的 TabbedView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆