1

Swift 5.x iOS 13/14

Trying to build a custom action sheet and mystified as to why this slides from the left and then up when I am telling it to simply slide from the bottom or at least I thought that was what I was asking.

import SwiftUI

struct ContentView: View {
@State private var showingCustomSheet = false
var body: some View {
    ZStack {
        Button(action: {
            self.showingCustomSheet.toggle()
        }) {
            Text("Fire")
              .font(.largeTitle)
              .foregroundColor(Color.red)
        }
        if showingCustomSheet {
          CustomAlertSheet(showingCustomSheet: $showingCustomSheet)
        }
    }
}
}

struct CustomAlertSheet: View {
@Binding var showingCustomSheet: Bool
@State var showEscape = false
var body: some View {
VStack {
  Spacer().onAppear {
    withAnimation(.linear(duration: 2)) {
        showEscape.toggle()
    }
  }
  if showEscape {
      Rectangle()
        .fill(Color.red)
        .frame(width: 256, height: 128)
        .transition(.move(edge: .bottom))
        .animation(.default)
  }
}
}
}

1 Answer 1

1

Here is a solution - removed redundant things and some small fixes... Tested with Xcode 12 / iOS 14.

demo

struct ContentView: View {
    @State private var showingCustomSheet = false

    var body: some View {
        ZStack {
            Button(action: {
                self.showingCustomSheet.toggle()
            }) {
                Text("Fire")
                    .font(.largeTitle)
                    .foregroundColor(Color.red)
            }
            CustomAlertSheet(showingCustomSheet: $showingCustomSheet)
        }
    }
}

struct CustomAlertSheet: View {
    @Binding var showingCustomSheet: Bool

    var body: some View {
        VStack {
            Spacer()
            if showingCustomSheet {
                Rectangle()
                    .fill(Color.red)
                    .frame(width: 256, height: 128)
                    .transition(.move(edge: .bottom))
            }
        }
        .animation(.default, value: showingCustomSheet)
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.