1

I have a SwiftUI app with a button that triggers a pop-up view using the .sheet() modifier. However, the pop-up view appears too large and I would like to make it smaller.

I tried wrapping the pop-up view in a VStack and setting a specific frame size using the .frame() modifier, but this did not change the size of the view.

Here is an example of my code:

struct ContentView: View {
    @State private var showingSmallView = false
    var body: some View {
        NavigationView {
            VStack {
                List {
                    Text("hi")
                    Text("hi")
                    Text("hi")
                }
                Button(action: { showingSmallView = true }){
                    Rectangle()
                        .frame(width: 50, height: 50)
                }
            }
        }
        .sheet(isPresented: $showingSmallView){
            VStack {
                smallView()
            }
            .frame(width: 200, height: 200)
        }
    }
}

struct smallView: View {
    var body: some View{
        Text("hda")
    }
}

I would appreciate any help or suggestions on how to make the pop-up view smaller.

starball
  • 20,030
  • 7
  • 43
  • 238
Adel
  • 61
  • 5

1 Answers1

1

You can use the .presentationDetents modifier. You can change the value to be whatever size you need. There are also non-custom values you can use (i.e. .medium and .large. See below:

    struct ContentView: View {
    @State private var showingSmallView = false
    var body: some View {
        NavigationView {
            VStack {
                List {
                    Text("hi")
                    Text("hi")
                    Text("hi")
                }
                Button(action: { showingSmallView = true }){
                    Rectangle()
                        .frame(width: 50, height: 50)
                }
            }
        }
        .sheet(isPresented: $showingSmallView){
            VStack {
                smallView()
                    .presentationDetents([.height(100)]) // here! 
            }
            .frame(width: 200, height: 200)
        }
    }
}

struct smallView: View {
    var body: some View{
        Text("hda")
    }
}
nickreps
  • 903
  • 8
  • 20