将图片和文本添加到操作表的警告按钮

8

我希望能够向 AlertButton 添加图标和文本,但该类型是 Text? 类型。

struct NavigationAddItem: View {
    @State var showActionView = true

    var actionSheet: ActionSheet {
        ActionSheet(title: Text("Select an action"), message: nil, buttons: [
            .default(Text("Option A"), action: {
                // TODO: Enter Option A action
            }),
            .default(Text("Option B"), action: {
                // TODO: Enter Option B action
            }),
            .cancel()
        ])
    }

    var body: some View {
        Button(action: {
            self.showActionView.toggle()
        }) {
            Image(systemName: "plus")
                .font(.system(size: 24))
        }
        .actionSheet(isPresented: $showActionView, content: {
            self.actionSheet
        })
    }
}


struct NavigationItems_Previews: PreviewProvider {
    static var previews: some View {
        NavigationAddItem(showActionView: true)
    }
}

这段代码可以显示操作表单,但我想在文本左侧添加一个图片。
当前效果: enter image description here 期望效果: enter image description here

你找到了可行的解决方案吗?如果是这样,能否与我们分享一下? - Jonas Deichelmann
1
我没有找到一个可行的解决方案。 - sfung3
1个回答

3

我认为目前无法为操作表中的按钮定义自己的视图。

查看 ActionSheet 的代码,我们可以看到以下内容:

@available(iOS 13.0, tvOS 13.0, watchOS 6.0, *)
@available(OSX, unavailable)
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
}

这表明 ActionSheet 使用的 Button 别名为 Alert.Button

如果我们查看 Alert 结构体,可以看到以下方法:

/// A storage type for an alert presentation.
@available(iOS 13.0, OSX 10.15, tvOS 13.0, watchOS 6.0, *)
public struct Alert {

    /// Creates an alert with one button.
    public init(title: Text, message: Text? = nil, dismissButton: Alert.Button? = nil)

    /// Creates an alert with two buttons.
    ///
    /// - Note: the system determines the visual ordering of the buttons.
    public init(title: Text, message: Text? = nil, primaryButton: Alert.Button, secondaryButton: Alert.Button)

    /// A button representing an operation of an alert presentation.
    public struct Button {

        /// Creates an `Alert.Button` with the default style.
        public static func `default`(_ label: Text, action: (() -> Void)? = {}) -> Alert.Button

        /// Creates an `Alert.Button` that indicates cancellation of some
        /// operation.
        public static func cancel(_ label: Text, action: (() -> Void)? = {}) -> Alert.Button

        /// Creates an `Alert.Button` that indicates cancellation of some
        /// operation.
        ///
        /// - Note: the label of the button is automatically chosen by the
        /// system for the appropriate locale.
        public static func cancel(_ action: (() -> Void)? = {}) -> Alert.Button

        /// Creates an `Alert.Button` with a style indicating destruction of
        /// some data.
        public static func destructive(_ label: Text, action: (() -> Void)? = {}) -> Alert.Button
    }
}

注意,创建Alert.Button的每个函数,它们分别是:defaultcanceldestructive,只接受类型为Text的标签和类型为(() -> Void)?action。这意味着你只能传递符合Text协议的视图。
因此,尝试传递不符合Text协议的视图是行不通的,正如你已经发现的那样。

1
@JonasDeichelmann 你可以自己实现一个模态框(因为目前 SwiftUI 中没有全屏模态框 - 你可以通过谷歌搜索找到很多教程),并在模态框内构建所需的 UI。虽然不是最理想的解决方案,但这是一种可行的方法。 - Andrew
谢谢。我希望有一个更好的解决方法。 - Jonas Deichelmann

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接