pushdeer/ios/PushDeer-iOS/PushDeer/Common/ViewExtension.swift

79 lines
2.1 KiB
Swift
Raw Normal View History

//
// ViewExtension.swift
// PushDeer
//
// Created by HEXT on 2022/2/12.
//
import SwiftUI
/// View ,
extension View {
/*
ps: #available(iOS 15.0, *) Xcode 13.2 bug
https://stackoverflow.com/questions/70506330/swiftui-app-crashes-with-different-searchbar-viewmodifier-on-ios-14-15/70603710#70603710
: if #available(iOS 15.0, *) { textField().submitLabel(.done) }
iOS 14 , submitLabel ,
submitLabel iOS 14 , .
, ViewModifier ,
aaa ViewModifier submitLabel, 使:
if #available(iOS 15.0, *) { textField().aaa(.done) }
aaa ViewModifier ,
*/
/// List
func refresh(action: @escaping @Sendable () async -> Void) -> some View {
Group {
if #available(iOS 15.0, *) {
self.modifier(RefreshModifier(action: action))
} else {
self
}
}
}
/// : /Done
func submitLabelDone() -> some View {
Group {
if #available(iOS 15.0, *) {
self.modifier(SubmitLabelDoneModifier())
} else {
self
}
}
}
}
@available(iOS 15.0, *)
struct RefreshModifier: ViewModifier {
let action: @Sendable () async -> Void
func body(content: Content) -> some View {
content
.refreshable(action: action)
}
}
@available(iOS 15.0, *)
struct SubmitLabelDoneModifier: ViewModifier {
func body(content: Content) -> some View {
content
.submitLabel(.done)
}
}
struct ViewExtension_Previews: PreviewProvider {
static var previews: some View {
List {
Text("A List Item")
Text("A Second List Item")
Text("A Third List Item")
TextField("请输入", text: .constant(""))
.submitLabelDone()
}
.refresh {
}
}
}