How to Create a Popup Menu in iOS

How can I create a popup menu in iOS?

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)

I need to create like a pop up menu where I can change data in a collection view when the collection view cell is pressed.

You can add a textfield in your alertController.

Please refer to this answer https://stackoverflow.com/a/33000328/1754559

let alertController = UIAlertController(title: "Your alert", message: "", preferredStyle: .alert)

let saveAction = UIAlertAction(title: "Save", style: .default, handler: {
alert -> Void in

guard let textField = alertController.textFields?[0] as UITextField else { return }
yourLabel.text = textField.text
})

let cancelAction = UIAlertAction(title: "Cancel", style: .default, handler: {
(action : UIAlertAction!) -> Void in })

alertController.addTextField { (textField : UITextField!) -> Void in
textField.placeholder = "Whatever"
//Other textField configurations
}

alertController.addAction(saveAction)
alertController.addAction(cancelAction)

present(alertController, animated: true, completion: nil)

Xcode - How to make a pop up menu

Here is an example of using UIActionSheet to present a popup menu in a UITextView in response to a longPress on an attachment.

- (void)attachmentActionSheet:(UITextView *)textView range:(NSRange)range {

// get the rect for the selected attachment (if its a big image with top not visible the action sheet
// will be positioned above the top limit of the UITextView
// Need to add code to adjust for this.
CGRect attachmentRect = [self frameOfTextRange:range inTextView:textView];

_attachmentMenuSheet = [[UIActionSheet alloc] initWithTitle:nil
delegate:self
cancelButtonTitle:@"Cancel"
destructiveButtonTitle:nil
otherButtonTitles:@"Copy Image", @"Save to Camera Roll", @"Open in Viewer", nil];

// Show the sheet
[_attachmentMenuSheet showFromRect:attachmentRect inView:textView animated:YES];
}

- (void)actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex {
if (actionSheet == _attachmentMenuSheet) {
//FLOG(@"Button %d", buttonIndex);
switch (buttonIndex) {

case 0:
//FLOG(@" Copy Image");
[self copyImageToPasteBoard:[_attachment image]];
break;

case 1:
//FLOG(@" Save to Camera Roll");
[self saveToCameraRoll:[_attachment image]];
break;

case 2:
//FLOG(@" Open in Viewer");
[self browseImage:[_attachment image]];
break;

default:
break;
}
}
}

And this is what it looks like on the iPad and iPhone (iOS7)

Sample Image

Sample Image

How to show popUp with opening/closing effects when selecting UIBarButtonItem from NavigationBar in Swift 5?

There is a new feature in iOS14 (UIKit and swiftUI) called pulldown menu or context menu. Menu can now added to UIButtons and UIBarbuttonItems.

let tbMenu = UIMenu(title: "", children: /* UIActions */)
UIBarButtonItem(image: UIImage(systemName: "list.number"), menu: buttonMenu)

Pull-down menus
Context menus (for TableViews)



Related Topics



Leave a reply



Submit