繁体   English   中英

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

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

我正在为 iPad 和 SwiftUI 开发一个应用程序。 我想展示一个页面,但尺寸较小。 我正在使用,但我无法更改页面的宽度。

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

结果是:

我的应用页面表

我怎样才能拥有 Apple 快捷方式应用程序模式大小?

快捷方式应用

也许这是 Apple 的专有自定义视图,UIKit 不可用...非常感谢

目前,您可以使用更小的 actionSheet(iOS 14 或更早版本)/confirmationDialog(iOS 15 或更高版本),或者您可以使用SheeKit创建自己的定制表单。

对于一个 actionSheet/confirmationDialog 就足够的情况:

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"
        }

    }

虽然我不太了解 SwiftUI ,但我知道您想要的纸张尺寸(如第二个示例所示)至少可以在UIKit中完成。

它被称为表单。 根据我对 SwiftUI 工作表的理解,它要呈现的工作表类型没有选项,在这种情况下,您示例中的工作表似乎等同于UIKit的页面表。

但是,如果您对UIKit方式感兴趣,请按以下方法操作:

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

基本上,您强制 View Controller 以表单样式呈现,然后呈现。

暂无
暂无

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

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