简体   繁体   English

如何展示带有 SwiftUI 的小页面?

[英]How can I present a small page sheet with SwiftUI?

I'm developing an app for iPad with SwiftUI.我正在为 iPad 和 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?我怎样才能拥有 Apple 快捷方式应用程序模式大小?

快捷方式应用

Maybe it's a proprietary custom view from Apple not available with UIKit... Thank you very much也许这是 Apple 的专有自定义视图,UIKit 不可用...非常感谢

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 .目前,您可以使用更小的 actionSheet(iOS 14 或更早版本)/confirmationDialog(iOS 15 或更高版本),或者您可以使用SheeKit创建自己的定制表单。

For the case that an actionSheet / confirmationDialog is enough:对于一个 actionSheet/confirmationDialog 就足够的情况:

Screenshot of the actionSheet 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 .虽然我不太了解 SwiftUI ,但我知道您想要的纸张尺寸(如第二个示例所示)至少可以在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.根据我对 SwiftUI 工作表的理解,它要呈现的工作表类型没有选项,在这种情况下,您示例中的工作表似乎等同于UIKit的页面表。

If you are interested in the UIKit way though, here's how:但是,如果您对UIKit方式感兴趣,请按以下方法操作:

// 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.基本上,您强制 View Controller 以表单样式呈现,然后呈现。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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