[SwiftUI] 토스트 뷰 생성 방법
사용하는 방법
struct ContentView: View {
@State var presentTop: Bool = false
var body: some View {
VStack {
TPButton(title: "☝️", action: {
presentTop = true
})
}
.navigationBarTitleDisplayMode(.inline) // if it's needed
.toast(isPresented: $presentTop, edge: .top) { toastContent }
}
var toastContent: some View {
HStack {
Image(systemName: "checkmark.circle").font(.headline)
Text("Toast!").font(.headline)
}
.frame(maxHeight: .infinity)
.padding()
.background(RoundedRectangle(cornerRadius: 8.0).foregroundColor(.random)) // set color whatever you want
}
3단계
1단계: ToastView 만들기
struct Toast<Content:View>: View {
@Binding var isPresented: Bool
@State var edge: Edge
var content: Content
let action: (() -> Void)?
private var alignment: Alignment {
switch edge {
case .top: return .top
case .leading: return .leading
case .bottom: return .bottom
case .trailing: return .trailing
}
}
init(isPresented: Binding<Bool>, edge: Edge, action: (() -> Void)? = nil, @ViewBuilder content: () -> Content) {
_isPresented = isPresented
_edge = State(wrappedValue: edge)
self.action = action
self.content = content()
}
var body: some View {
ZStack(alignment: alignment) {
Spacer().frame(maxWidth: .infinity, maxHeight: .infinity)
content
}
.frame(maxWidth: .infinity, maxHeight: .infinity)
.padding()
.animation(.spring(), value: isPresented)
.transition(.move(edge: edge).combined(with: .opacity))
.onTapGesture {
withAnimation {
isPresented = false
action?()
}
}
}
}
2단계: ViewModifier 생성
struct ToastModifier<ToastContent: View>: ViewModifier {
@Binding var isPresented: Bool
var toast: Toast<ToastContent>
init(isPresented: Binding<Bool>, edge: Edge, action: (() -> Void)? = nil, @ViewBuilder toastContent: () -> ToastContent) {
_isPresented = isPresented
toast = Toast(isPresented: isPresented, edge: edge, action: action, content: toastContent)
}
public func body(content: Content) -> some View {
ZStack {
content
if isPresented { toast }
}
.frame(maxWidth: .infinity, maxHeight: .infinity)
.background(Color.clear.ignoresSafeArea(.all, edges: .all))
.animation(.spring(), value: isPresented)
}
}
3단계: 확장 보기
public extension View {
func toast<Content:View>(isPresented: Binding<Bool>, edge: Edge, action: (() -> Void)? = nil, @ViewBuilder content: () -> Content) -> some View {
self.modifier(ToastModifier(isPresented: isPresented, edge: edge, action: action, toastContent: content))
}
}
Reference
이 문제에 관하여([SwiftUI] 토스트 뷰 생성 방법), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://dev.to/shohe/swiftui-toast-view-27cd텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념 (Collection and Share based on the CC Protocol.)