如何创建像 WhatsApp 中那样的弹出菜单?
很抱歉问了这个愚蠢的问题,但我什至不知道要搜索什么。我很确定它不是 UIPickerView
。
最佳答案
这是一个操作表。 Here's the documentation有关它的信息,请参阅 iOS 人机界面指南。
你可以像这样制作一个:
SwiftUI(iOS 15 及更高版本)
使用confirmationDialog()
。 Here is the official documentation for it和 here 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/