如何创建类似 WhatsApp 中的弹出菜单?
抱歉问一个傻问题,但我甚至不知道该搜索什么。我很确定它不是 UIPickerView。
抱歉问一个傻问题,但我甚至不知道该搜索什么。我很确定它不是 UIPickerView。
confirmationDialog()
。这里是官方文档,这里有一些现实世界的例子,部分是示例代码的来源。
@State private var shouldShowActionSheet = false
<custom view>
.confirmationDialog("", isPresented: $shouldShowActionSheet) {
Button("Option 1") {
<handler>
}
Button("Option 2") {
<handler>
}
Button("Cancel", role: .cancel) { }
}
@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()
]
)
}
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)
.cancel
操作。只需添加一个样式为.cancel
的操作。重要的是,这也解决了当用户单击“屏幕上的任何其他位置”以关闭弹出窗口时的问题。 - Fattie它是使用preferredStyle属性的UIAlertController
- UIAlertControllerStyle.actionSheet
https://developer.apple.com/documentation/uikit/uialertcontroller