如何使用仅适用于 iOS 14+ 且部署目标为 iOS 13 的视图修饰符
How to use view modifiers only available for iOS 14+ with deployment target of iOS 13
当视图修饰符仅在 iOS 14 可用但您的应用程序可用于 iOS 13 时,如何向视图添加视图修饰符?
举个例子,textCase(_)
。 iOS 14 中的 header 部分是大写的,因此要使用 header 文本的大小写,您应该在 Text
上设置 .textCase(.none)
,但这并没有一直存在到 iOS 14.
Section(header:
Text("Header")
.textCase(.none) //FIXME: 'textCase' is only available in iOS 14.0 or newer
.font(.system(size: 14, weight: .bold))
.foregroundColor(.secondary)
.padding(.top, 50)
)
Xcode 提供一些建议:
- 如果#available 版本检查添加
- 添加@available 属性
如果您使用#available 版本检查,它会用#available 包装所有范围内的代码,因此您必须复制所有这些代码才能添加那一行代码。如果使用@available,则必须复制整个 body 属性 或整个结构。
我考虑过创建自己的 ViewModifier,它只会在 iOS 14 时应用它,但会出现这个可怕的错误:
Function declares an opaque return type, but the return statements in
its body do not have matching underlying types
struct CompatibleTextCaseModifier: ViewModifier {
func body(content: Content) -> some View {
if #available(iOS 14.0, *) {
return content
.textCase(.none)
} else {
return content
}
}
}
将 body
标记为 @ViewBuilder
- 这将允许自动跟踪内部不同的 return 类型,并删除 return
因为显式 return 禁用视图构建器包装器.
所以这里是固定变体
struct CompatibleTextCaseModifier: ViewModifier {
@ViewBuilder
func body(content: Content) -> some View {
if #available(iOS 14.0, *) {
content
.textCase(.none)
} else {
content
}
}
}
和用法
Section(header:
Text("Header")
.modifier(CompatibleTextCaseModifier())
.font(.system(size: 14, weight: .bold))
.foregroundColor(.secondary)
.padding(.top, 50)
) {
Text("test")
}
当视图修饰符仅在 iOS 14 可用但您的应用程序可用于 iOS 13 时,如何向视图添加视图修饰符?
举个例子,textCase(_)
。 iOS 14 中的 header 部分是大写的,因此要使用 header 文本的大小写,您应该在 Text
上设置 .textCase(.none)
,但这并没有一直存在到 iOS 14.
Section(header:
Text("Header")
.textCase(.none) //FIXME: 'textCase' is only available in iOS 14.0 or newer
.font(.system(size: 14, weight: .bold))
.foregroundColor(.secondary)
.padding(.top, 50)
)
Xcode 提供一些建议:
- 如果#available 版本检查添加
- 添加@available 属性
如果您使用#available 版本检查,它会用#available 包装所有范围内的代码,因此您必须复制所有这些代码才能添加那一行代码。如果使用@available,则必须复制整个 body 属性 或整个结构。
我考虑过创建自己的 ViewModifier,它只会在 iOS 14 时应用它,但会出现这个可怕的错误:
Function declares an opaque return type, but the return statements in its body do not have matching underlying types
struct CompatibleTextCaseModifier: ViewModifier {
func body(content: Content) -> some View {
if #available(iOS 14.0, *) {
return content
.textCase(.none)
} else {
return content
}
}
}
将 body
标记为 @ViewBuilder
- 这将允许自动跟踪内部不同的 return 类型,并删除 return
因为显式 return 禁用视图构建器包装器.
所以这里是固定变体
struct CompatibleTextCaseModifier: ViewModifier {
@ViewBuilder
func body(content: Content) -> some View {
if #available(iOS 14.0, *) {
content
.textCase(.none)
} else {
content
}
}
}
和用法
Section(header:
Text("Header")
.modifier(CompatibleTextCaseModifier())
.font(.system(size: 14, weight: .bold))
.foregroundColor(.secondary)
.padding(.top, 50)
) {
Text("test")
}