10

有什么方法可以在操作表中没有标题部分?SwiftUI 代码中的 Action Sheet 定义如下:

public struct ActionSheet {
   /// Creates an action sheet with the provided buttons.
   public init(title: Text, message: Text? = nil, buttons: [ActionSheet.Button] = [.cancel()])
   /// A button representing an operation of an action sheet presentation.
   public typealias Button = Alert.Button
}

由于 title 只符合 Text,我想也许只添加 Text("") 就可以了;但是,这反而使标题的空间保持空白,而不是删除它。给 nil 而不是 Text("") 也不起作用。

此外,是否有任何方法可以为操作表的按钮提供视图,如下所示:

struct ActionItem: View {

  var body: some View {

    HStack {

        Image(systemName: "phone")

        Spacer()

        Text("Call 1-408-123-4567")
    }
  }
}
4

2 回答 2

16

最简洁的答案是不。SwiftUI 当前的实现将始终为标题腾出空间,并且只会Text为其按钮获取视图。

目前尚不清楚这是否是因为 Apple 在今年发布 SwiftUI 之前只有这么多时间,并且想要解决最简单和最常见的用例,或者他们是否采取了 ActionSheets 应该始终具有标准外观的原则立场,包括标题和只有文本按钮。我们将不得不拭目以待。

于 2019-11-06T03:30:49.137 回答
0

confirmationDialogApple 已通过 iOS 15 和新引入的API接听了我们的电话。ActionSheet在此版本中也已弃用。

ConfirmationDialog已添加为视图修饰符,可用于任何视图,与.actionSheet我们之前使用的 API 非常相似。当使用新对话框时,我们可以指定标题是隐藏的,并且可以使用 Button APIrole来控制按钮的外观。

下面是在一个简单的 View 上使用它的图示。

struct ContentView: View {
    
    @State private var isShowingDialog: Bool = false
    
    var body: some View {
        VStack {
            Button {
                isShowingDialog = true
            } label: {
                Text("Tap Me")
            }
             .confirmationDialog("", isPresented: $isShowingDialog, titleVisibility: .hidden) {
                 Button("Normal") {
                 }
                 Button("Delete", role: .destructive) {
                 }
                 Button("Cancel", role: .cancel) {
                 }
            }
        }
    }
}

在此处输入图像描述

于 2022-03-04T08:21:06.850 回答