简体   繁体   中英

How can I present a small page sheet with SwiftUI?

I'm developing an app for iPad with SwiftUI. I want to present a page sheet but with a smaller size. I'm using but I can't change the width of the page sheet.

.sheet(
    isPresented: self.$isEditing,
) {
    Text("My page sheet view")
}

The result is:

我的应用页面表

How can I have the Apple shortcut app modal size?

快捷方式应用

Maybe it's a proprietary custom view from Apple not available with UIKit... Thank you very much

At the moment, you can use an actionSheet (iOS 14 or earlier) / confirmationDialog (iOS 15 or later) which is much smaller or you can create your own custom-made sheet maybe with usage of SheeKit .

For the case that an actionSheet / confirmationDialog is enough:

Screenshot of the actionSheet

@State private var showingConfirm: Bool = false
@State private var actionSelection = ""
.. 

Group {
    Button(action: {
            showingConfirm.toggle()
        }, label: {
            Label("Demo")
        })
 }
// iOS 14 
.actionSheet(isPresented: $showingConfirm, content: {

        let action1 = ActionSheet.Button.default(Text("First action")) {
            actionSelection = "First"
        }

        let action2 = ActionSheet.Button.default(Text("Second action")) {
            actionSelection = "Second"
        }

        return ActionSheet(title: Text("Action Sheet"), message: Text("Message"), buttons: [action1, action2])
 })

 // or for iOS 15
 .confirmationDialog("Action Sheet", isPresented: $showingConfirm, titleVisibility: .visible) {
        Button("First action") {
            actionSelection = "First"
        }

        Button("Second action") {
            actionSelection = "Second"
        }

    }

While I do not know SwiftUI that much, I do know that the size of the sheet that you wanted, as shown in the second example, can be done, at least in UIKit .

Its called a form sheet. From my understanding of SwiftUI sheets, there is no option for the type of sheet it is to be presenting, at which, the one in your example seems equivalent to UIKit 's page sheet.

If you are interested in the UIKit way though, here's how:

// self shall be your ViewController
self.modalPresentationStyle = .formSheet
self.present(yourSheetViewController, animated: true, completion: nil)

Basically you force the View Controller to present in form sheet style, and then present it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM