admin管理员组文章数量:1357672
I've been meaning to try .containerBackground
for a while, having seen it introduced with iOS 17, but now that I did I am left confused, because XCode claims it's available only on iOS 18 or newer:
Compiling failed: 'navigation' is only available in iOS 18.0 or newer
However, the documentation clearly states iOS 17+. I looked it up and I haven't found anything about it not being available in iOS 17+.
So am I using it wrong, or what's going on here?
Here's an MRE, assuming your project is setup to target iOS 17+, in order to get the error.
import SwiftUI
struct ContainerBackgroundTest: View {
var body: some View {
NavigationStack {
// if #available(iOS 18.0, *) {
VStack {
ContentUnavailableView {
Label("containerBackground(_:for:)", systemImage: "bubbles.and.sparkles")
} description: {
let availability = """
iOS 17.0+ | iPadOS 17.0+ | Mac Catalyst 17.0+ | macOS 14.0+ | tvOS 17.0+ | visionos 1.0+ | watchOS 10.0+
"""
Text("Sets the container background of the enclosing container using a view.")
Text(availability)
} actions: {
Button {
//action here...
} label: {
Text("Report bad documentation")
}
}
}
.navigationTitle("Home")
.containerBackground(.cyan.gradient.opacity(0.6), for: .navigation)
// } else {
// // Fallback on earlier versions
// }
}
}
}
#Preview {
ContainerBackgroundTest()
}
I've been meaning to try .containerBackground
for a while, having seen it introduced with iOS 17, but now that I did I am left confused, because XCode claims it's available only on iOS 18 or newer:
Compiling failed: 'navigation' is only available in iOS 18.0 or newer
However, the documentation clearly states iOS 17+. I looked it up and I haven't found anything about it not being available in iOS 17+.
So am I using it wrong, or what's going on here?
Here's an MRE, assuming your project is setup to target iOS 17+, in order to get the error.
import SwiftUI
struct ContainerBackgroundTest: View {
var body: some View {
NavigationStack {
// if #available(iOS 18.0, *) {
VStack {
ContentUnavailableView {
Label("containerBackground(_:for:)", systemImage: "bubbles.and.sparkles")
} description: {
let availability = """
iOS 17.0+ | iPadOS 17.0+ | Mac Catalyst 17.0+ | macOS 14.0+ | tvOS 17.0+ | visionos 1.0+ | watchOS 10.0+
"""
Text("Sets the container background of the enclosing container using a view.")
Text(availability)
} actions: {
Button {
//action here...
} label: {
Text("Report bad documentation")
}
}
}
.navigationTitle("Home")
.containerBackground(.cyan.gradient.opacity(0.6), for: .navigation)
// } else {
// // Fallback on earlier versions
// }
}
}
}
#Preview {
ContainerBackgroundTest()
}
Share
Improve this question
asked Mar 27 at 20:29
Andrei G.Andrei G.
1,7902 gold badges10 silver badges13 bronze badges
2
|
1 Answer
Reset to default 0The error message made this very clear. It is navigation
that is unavailable, not the .containerBackground
modifier itself.
In iOS 17, you can still use .containerBackground
to modify the background of other ContainerBackgroundPlacement
s. In fact, Xcode's template for a widget extension includes
.containerBackground(.fill.tertiary, for: .widget)
The other 3 ContainerBackgroundPlacements
available on iOS 17 are:
subscriptionStore
subscriptionStoreFullHeight
subscriptionStoreHeader
which are all a part of StoreKit.
本文标签: swiftuicontainerBackground availability incorrectStack Overflow
版权声明:本文标题:swiftui - .containerBackground availability incorrect? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744069290a2585592.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
ContainerBackgroundPlacement.navigation
that is only available in iOS 18. Other kinds of placement are available in iOS 17. For example, if youimport StoreKit
then.subscriptionStore
is available. – Benzy Neez Commented Mar 27 at 21:44