Hi,
I'm adding tabs to the iOS version of my multiplatform app using TabView. I want the individual tabs to have names and icons. In iOS 17 and below, I have to do this using:
tabContent().tabItem {
Label(titleKey, systemImage: systemImage)
}
but this is deprecated, so in iOS 18 I would like to use the new version:
Tab(titleKey, image: systemImage) {
content()
}
It would be annoying to have to have the two cases for each individual tab, so I'm trying to abstract it into a custom SwiftUI view like this:
var body: some View {
if #available(iOS 18.0, *) {
Tab(titleKey, image: systemImage) {
content()
}
} else {
content().tabItem {
Label(titleKey, systemImage: systemImage)
}
}
}
There's a bit more to the custom view because I also have cases for iPad and macOS where I just have the views next to each other without tabs, but that's not really relevant to the question other than providing further motivation for abstracting this.
However, with this code, I get the error:
'buildExpression' is unavailable: this expression does not conform to 'View'
on the Tab line, because Tab isn't a view, and it can only be used directly inside a TabView.
For now at least, I can just use tabItem on all iOS versions and it works, but I'd prefer not to in case it is removed some time soon. I do want to support iOS 17 because that's what my iPad runs. Is there any clean way to do this?
1
0
249