[英]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") } }
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: "")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.