简体   繁体   English

如何使用SwiftUI向标签栏项添加图像和标签

[英]How do I add an image and a label to a tab bar item with SwiftUI

Using this code I can set the label or the image but am unclear how to set both. 使用此代码,我可以设置标签或图像,但不清楚如何设置两者。

    var body: some View {
        Text("Library")
            .font(.title)
//            .tabItemLabel(Image("first"))
            .tabItemLabel(Text("Library"))
            .tag(0)
    }

In the SwiftUI Essentials WWDC talk, they use this sample code to have both image and label in tab bar item: 在SwiftUI Essentials WWDC演讲中,他们使用以下示例代码在标签栏项中同时包含图像和标签:

 TabbedView { OrderForm() .tabItemLabel { Image(systemName: "square.and.pencil") Text("New Order") } OrderHistory() .tabItemLabel { Image(systemName: "clock.fill") Text("History") } } 
But at this moment using this approach to add `tabItemLabel` in Xcode 11 beta throws a compiler error and it cannot be used. 但是目前使用这种方法在Xcode 11 beta中添加`tabItemLabel`会引发编译器错误,因此无法使用。 So it might be a bug and will probably fix in next releases. 因此,这可能是一个错误,可能会在以后的版本中修复。

Update: 更新:

This issue was fixed with Xcode 11 beta 3 . Xcode 11 beta 3已解决此问题。 tabItemLabel is renamed to tabItem and can be used like below: tabItemLabel重命名为tabItem ,可以如下使用:

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

There is a workaround on the officially Release Notes of iOS 13 Beta 2: iOS 13 Beta 2的正式发行说明中有解决方法:

Workaround: Wrap the views you pass to the modifier in a VStack: 解决方法:在VStack中包装传递给修改器的视图:

MyView()
    .tabItemLabel(VStack {
        Image("resourceName")
        Text("Item")
    })

Note: this workaround does not work with Image(systemImage: "") 注意:此解决方法不适用于Image(systemImage: "")

Now that Xcode 11 GM has been released, here's how you can implement a TabView item, with images & labels 现在,Xcode 11 GM已经发布,这是您如何实现带有图像和标签的TabView项的方法


.tabItem {
   Image(systemName: "phone.fill")
   Text("First Tab")
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM