Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
890 views
in Technique[技术] by (71.8m points)

swift - How can I create a popup menu in iOS?

How can I create a popup menu like the one present in WhatsApp?

Screenshot of the popup menu

Sorry for the dumb question, but I don't even know what to search. I'm pretty sure it's not a UIPickerView.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

This is an action sheet. Here's the documentation about it in the iOS Human Interface Guidelines.

You can make one like this:

SwiftUI (iOS 15 and above)

Use confirmationDialog(). Here is the official documentation for it and 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 and 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 1", style: .default) { _ in
        <handler>
    }
)

present(alert, animated: true)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...