2

I am trying to present Model view with small size. Is there any way to resize it.

Button("Present") { self.presentingModal = true }
    .padding()
    .sheet(isPresented: $presentingModal) {} 
   content: {
        ModelView(presentedAsModal: $presentingModal)
            }

It presents the model with with default size. I am not able to resize it.

PS: Our goal is to present Modal at centre with custom size.

Santosh Singh
  • 765
  • 11
  • 27
  • Does this answer your question? [SwiftUI - Half modal?](https://stackoverflow.com/questions/56700752/swiftui-half-modal) – lorem ipsum Jan 04 '22 at 21:52
  • Actually No, Our goal is to present Modal at centre with custom size. – Santosh Singh Jan 05 '22 at 07:21
  • You can easily achieve this in a similar manner to the post. Just set the background colors in UIKit to nil and add constraints to position the hosting controller. There are also posts out there to create custom sized popovers – lorem ipsum Jan 05 '22 at 11:02

2 Answers2

3

You can overlay a resized one over the current View with a button tap.

import SwiftUI

struct SheetView: View {
    @State private var showSheet: Bool = false
    var body: some View {
        NavigationView {
            VStack {
                ZStack {
                    Button("Please select a mailing address") {
                        showSheet.toggle()
                    }.foregroundColor(Color.black)
                }
                Spacer()
            }
            .navigationBarTitleDisplayMode(.inline)
            .navigationViewStyle(StackNavigationViewStyle())
            //.ignoresSafeArea()
        }
        .overlay(popOver)
    }
    
    var popOver: some View {
        Group {
            if showSheet {
                ZStack(alignment: .bottom) {
                    Color.black.opacity(0.4).ignoresSafeArea()
                        .onTapGesture {
                            showSheet.toggle()
                        }
                    ZStack {
                        Rectangle()
                            .fill(Color.white)
                            .frame(maxWidth: .infinity, maxHeight: 340.0)
                            .cornerRadius(20.0, corners: [.topLeft, .topRight])
                        HelloView()
                    }
                }
            }
        }
    }
}

struct HelloView: View {
    var body: some View {
        Text("Hello")
    }
}
1

Not in pure SwiftUI. I think you'd have to use UIKit via UIViewControllerRepresentable. The best you can do in SwiftUI is modify the contents of the modal, e.g:

struct ContentView: View {
        @State var showSheet = false
        var body: some View {
            VStack {
                Button("Show sheet") {
                    showSheet = true
                }
            }
            .sheet(isPresented: $showSheet) {
                SheetView()
            }
        }
    }
    struct SheetView: View {
        @State var largerSheet = false
        var body: some View {
            VStack {
                Toggle("Larger Sheet", isOn: $largerSheet)
                if largerSheet {
                  ZStack {
                    Text("Larger")
                    Rectangle().frame(width: 300, height: 600).foregroundColor(.green).opacity(0.3)
                  }
                } else {
                  ZStack {
                  Text("Smaller")
                    Rectangle().frame(width: 200, height: 300).foregroundColor(.blue).opacity(0.3)
                  }
                }
            }
            .padding()
        }
    }
CALL-151
  • 506
  • 3
  • 12
  • Thank you very quick response. I tried UIViewControllerRepresentable but its not working. Since we need to set modalPresentationStyle to popOver and we need to handle UIPopoverPresentationControllerDelegate to control presentation style. I am able to resize same in UIKit(Swift). I see two issues with UIViewControllerRepresentable 1. Not sure how to set sourceRect 2. popoverPresentationController delete not invoked – Santosh Singh Jan 04 '22 at 18:01
  • 2
    @SantoshSingh SwiftUI is fun...until it isn't. Here's an approach that may work for you. https://github.com/edudnyk/SheeKit – CALL-151 Jan 05 '22 at 14:21