본문 바로가기
카테고리 없음

SwiftUI를 이용한 하단 알림 메시지 (ActionSheet)

by IFLA 2022. 11. 1.

Alert 와 유사하게 동작된다. Alert와 마찬가지로 알림창을 보여줄 지 말지를 결정할 Bool 타입 변수를 정의해야 한다. 조건이 true가 되면 ActionSheet가 하단에 표시된다.

 

  • MacOS에는 ActionSheet를 이용할 수 없다.

 

기본코드

import SwiftUI

struct ContentView: View {
    @State private var showActionSheet = false
    var body: some View {
        Button("Tap to show action sheet") {
            showActionSheet = true
        }
        .actionSheet(isPresented: $showActionSheet) {
            ActionSheet(title: Text("Resume Workout Recording"),
                        message: Text("Choose a destination for workout data"),
                        buttons: [
                            .cancel(),
                            .destructive(
                                Text("Overwrite Current Workout"),
                                action: {
                                    print("Overwrite")
                                }
                            ),
                            .default(
                                Text("Append to Current Workout"),
                                action: {
                                    print("Append!")
                                }
                            )
                        ]
            )
        }
    }
}

 

실행 결과

  • 현재는 ActionSheet가 deprecated가 되어 다른 방법으로 하단의 알림메시지창을 사용해야한다.

 

변경된 ActionSheet 코드

import SwiftUI

struct ContentView: View {
    @State var showingSheet = false

    var body: some View {
        Button(action: {
            self.showingSheet = true
        }) {
            Text("Action Sheet 띄우기")
        }
        .confirmationDialog("타이틀", isPresented: $showingSheet) {
          Button("제거", role: .destructive) {}
          Button("취소", role: .cancel) {}
        }
    }
}

 

실행 결과

개발자 Document

actionsheet에 관한 문서

Apple Developer Documentation

 

confirmationDialog에 관한 문서

https://developer.apple.com/documentation/swiftui/view/confirmationdialog(_:ispresented:titlevisibility:actions:)-46zbb

댓글


\