簡體   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