0
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

[SwiftUI] sheet modifierは1つしか持つことができないらしい

Last updated at Posted at 2021-05-25

やりたいこと

  • NavigationBarItemとFloating action buttonでそれぞれシートをpresentする

とりあえず実装してみた

Boolの@Stateを2つsheet modifierを2つ並べとけばええやろ

struct SomeView: View {
    @State private var isScheduling: Bool
    @State private var isSharing: Bool

    var body: some View {
        VStack(alignment: .leading) {
            // ...

            // fab
            HStack {
                Spacer()
                Button {
                    isScheduling.toggle()
                } label: {
                    Image(systemName: "plus")
                        .foregroundColor(.white)
                        .font(.title2)
                        .shadow(radius: 3)

                }
                .frame(width: 60, height: 60)
                .background(Color.primary)
                .cornerRadius(30.0)
                .shadow(color: .gray, radius: 3, x: 0, y: 0)
                .padding(.bottom, 16)
            }
        }
        .navigationBarItems(trailing: Button {
            isSharing.toggle()
        } label: {
            Image(systemName: "person.badge.plus")
        })
        .sheet(isPresented: $isScheduling) {
            ScheduleView()
        }
        .sheet(isPresented: $isSharing) {
            ShareView()
        }
    }
}

問題点

  • これだと後から宣言されたmodifier(ここではshareの方)だけが生きて、先に宣言した方は死ぬ
  • どうやらViewはひとつのsheet modifierしか持つことができないらしい

改善

struct SomeView: View {
    private enum ActiveSheet: Identifiable {
        case schedule
        case share

        var id: Int {
            hashValue
        }
    }

    @State private var activeSheet: ActiveSheet?

    var body: some View {
        VStack(alignment: .leading) {
            // ...

            // fab
            HStack {
                Spacer()
                Button {
                    activeSheet = .schedule
                } label: {
                    Image(systemName: "plus")
                        .foregroundColor(.white)
                        .font(.title2)
                        .shadow(radius: 3)

                }
                .frame(width: 60, height: 60)
                .background(Color.primary)
                .cornerRadius(30.0)
                .shadow(color: .gray, radius: 3, x: 0, y: 0)
                .padding(.bottom, 16)
            }
        }
        .navigationBarItems(trailing: Button {
            activeSheet = .share
        } label: {
            Image(systemName: "person.badge.plus")
        })
        .sheet(item: $activeSheet) { item in
            switch item {
            case .schedule:
                ScheduleView()
            case .share:
                ShareView()
            }
        }
    }
}

これで2つのsheetがpresentできるようになる
あとすっきりした


2021/5/9 追記

一つの子Viewに対しては1つ以上のsheetfullScreenCoverは持てないが、別のViewに持たせてしまえばいくらでも追加できた

参考

0
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?