英文:
Cannot convert value of type 'Image' to expected argument type 'String'
问题
I make a category included image scroll but I can't put my variable named tabBarItemName name into image, it keeps giving error like this: Cannot convert value of type 'Image' to expected argument type 'String'
struct ContentView: View {
@State var currentTab: Int = 0
var body: some View {
ZStack(alignment: .top) {
TabView(selection: self.$currentTab) {
Image1View().tag(0)
Image2View().tag(1)
Image3View().tag(2)
}
.tabViewStyle(.page(indexDisplayMode: .never))
.edgesIgnoringSafeArea(.all)
.padding(.top, 76)
CategoryScroll(currentTab: self.$currentTab)
}
}
}
struct CategoryScroll: View {
var tabBarOptions: [String] = ["bim"]
@Binding var currentTab: Int
var body: some View {
ScrollView(.horizontal) {
HStack(spacing: 20) {
ForEach(Array(zip(self.tabBarOptions.indices, self.tabBarOptions)),
id: \.0,
content: { index, name in
CategoryView(currentTab: self.$currentTab, tab: index, tabBarItemName: name)
})
}
}
.padding(7)
.background(Color.white)
}
}
struct CategoryView: View {
@Binding var currentTab: Int
var tab: Int
var tabBarItemName: String
var body: some View {
Button {
self.currentTab = tab
} label: {
VStack {
Text(tabBarItemName) // Changes made here
.frame(width: 12, height: 12)
.padding(.leading, 65)
if currentTab == tab {
Color.init(red: 0.965, green: 0.224, blue: 0.49)
.frame(height: 2)
.padding(.leading, 65)
} else {
Color.clear.frame(height: 6)
}
}
.animation(.spring(), value: self.currentTab)
}
.buttonStyle(.plain)
}
}
I have made changes to your code to resolve the error. Specifically, I changed the tabBarItemName
variable from Image
type to String
type in the CategoryView
struct, and I updated the Image
view to Text
view in the CategoryView
as well.
英文:
I make a category included image scroll butI can't put my variable named tabBarItemName name into image, it keeps giving error like this : Cannot convert value of type 'Image' to expected argument type 'String'
struct ContentView: View {
@State var currentTab: Int = 0
var body: some View {
ZStack(alignment:.top) {
TabView(selection:self.$currentTab){
Image1View().tag(0)
Image2View().tag(1)
Image3View().tag(2)
}
.tabViewStyle(.page(indexDisplayMode: .never))
.edgesIgnoringSafeArea(.all)
.padding(.top,76)
CategoryScroll(currentTab: self.$currentTab)
}
}
}
struct CategoryScroll: View {
var tabBarOptions: [Image] = [Image("bim")]
@Binding var currentTab : Int
var body: some View {
ScrollView(.horizontal){
HStack(spacing:20){
ForEach(Array(zip(self.tabBarOptions.indices, self.tabBarOptions)),
id:\.0,
content: {
index, name in
CategoryView(currentTab: self.$currentTab, tab: index, tabBarItemName: name)
})
}
}.padding(7)
.background(Color.white)
}
struct CategoryView: View {
@Binding var currentTab: Int
var tab : Int
var tabBarItemName: Image
var body: some View{
Button{
self.currentTab = tab
} label: {
VStack{
Image(tabBarItemName) // I'm trying to make changes right here
.frame(width: 12, height: 12)
.padding(.leading, 65)
if currentTab == tab {
Color.init( red: 0.965, green: 0.224, blue: 0.49)
.frame(height: 2)
.padding(.leading,65)
} else {
Color.clear.frame(height: 6)
}
}
.animation(.spring(), value: self.currentTab)
}
.buttonStyle(.plain)
}
}
}
I would be very happy if you could help me with this.
答案1
得分: 0
将以下内容进行翻译:
将
var tabBarOptions: [Image] = [Image("bim")]
改为
var tabBarOptions: [String] = ["bim"]
并将
var tabBarItemName: Image
改为
var tabBarItemName: String
问题:
Image(tabBarItemName)
期望的是一个字符串,而不是一个图像。
英文:
Change
var tabBarOptions: [Image] = [Image("bim")]
to
var tabBarOptions: [String] = ["bim"]
And change
var tabBarItemName: Image
to
var tabBarItemName: String
The issue:
Image(tabBarItemName) is expecting a String, not an Image
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论