ios - 如何在 iOS 中创建弹出菜单?

标签 ios swift

如何创建像 WhatsApp 中那样的弹出菜单?

Screenshot of the popup menu

很抱歉问了这个愚蠢的问题,但我什至不知道要搜索什么。我很确定它不是 UIPickerView

最佳答案

这是一个操作表Here's the documentation有关它的信息,请参阅 iOS 人机界面指南。

你可以像这样制作一个:

SwiftUI(iOS 15 及更高版本)

使用confirmationDialog()Here is the official documentation for ithere are some real-world examples, which are partially the source of the example code.

@State private var shouldShowActionSheet = false

<custom view>
.confirmationDialog("", isPresented: $shouldShowActionSheet) {
    Button("Option 1") {
        <handler>
    }

    Button("Option 2") {
        <handler>
    }

    Button("Cancel", role: .cancel) { }
}

SwiftUI(iOS 13 和 14)

@State private var shouldShowActionSheet = false

[...]

<custom view>
.actionSheet(isPresented: $shouldShowActionSheet) {
    ActionSheet(
        title: Text(""),
        buttons: [
            .default(Text("Option 1")) {
                <handler>
            },
            .default(Text("Option 2")) {
                <handler>
            },
            .cancel()
        ]
    )
}

UIKit

let alert = UIAlertController(
    title: nil,
    message: nil, 
    preferredStyle: .actionSheet
)

alert.addAction(
    .init(title: "Action 1", style: .default) { _ in
        <handler>
    }
)

alert.addAction(
    .init(title: "Action 2", style: .default) { _ in
        <handler>
    }
)

alert.addAction(.init(title: "Cancel", style: .cancel))

present(alert, animated: true)

关于ios - 如何在 iOS 中创建弹出菜单?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46633499/

相关文章:

ios - 使用 phonegap 在 IOS 的图库中使用 FileWriter 查看写入文件系统的图像文件

iOS应用程序,如何知道在ios appstore中使用哪个帐户购买应用程序?

ios - Swift - 不能使 scrollview 能够向左滚动吗?

ios - 获得 CGVector 的负数

swift - 如何在 swift 中将数据转换为十六进制字符串

ios - 由于未捕获的异常 'NSInvalidArgumentException' 而终止应用程序 ...原因( key : networkCarrier)

iOS 设计指南 - 主/第一个选择屏幕的选项卡控件与 ListView

ios - 将 NSLayoutConstraint 常量重置为其 Storyboard 中的原始值

ios - 使用swift制作下拉列表?

ios - Swift - 以编程方式添加自定义 Xib View 作为 subview