UIActionSheet iOS Swift

How to make UIActionSheet in iOS Swift? Here is my code for encoding UIActionSheet.

@IBAction func downloadSheet(sender: AnyObject) { let optionMenu = UIAlertController(title: nil, message: "Choose Option", preferredStyle: .ActionSheet) let saveAction = UIAlertAction(title: "Save", style: .Default, handler: { (alert: UIAlertAction!) -> Void in println("Saved") }) let deleteAction = UIAlertAction(title: "Delete", style: .Default, handler: { (alert: UIAlertAction!) -> Void in println("Deleted") }) let cancelAction = UIAlertAction(title: "Cancel", style: .Cancel, handler: { (alert: UIAlertAction!) -> Void in println("Cancelled") }) optionMenu.addAction(deleteAction) optionMenu.addAction(saveAction) optionMenu.addAction(cancelAction) self.presentViewController(optionMenu, animated: true, completion: nil) } 

Hope my code is clear ... I welcome the best suggestion for this code.

+81
ios swift uiactionsheet
Apr 27 '15 at 5:29
source share
14 answers

Your approach is suitable, but you can easily add a UIActionSheet another way.

You can add UIActionSheetDelegate to UIViewController` as

 class ViewController: UIViewController ,UIActionSheetDelegate 

Set a method e.g.

 @IBAction func downloadSheet(sender: AnyObject) { let actionSheet = UIActionSheet(title: "Choose Option", delegate: self, cancelButtonTitle: "Cancel", destructiveButtonTitle: nil, otherButtonTitles: "Save", "Delete") actionSheet.showInView(self.view) } 

You can get your button index when it clicked, like

 func actionSheet(actionSheet: UIActionSheet, clickedButtonAtIndex buttonIndex: Int) { println("\(buttonIndex)") switch (buttonIndex){ case 0: println("Cancel") case 1: println("Save") case 2: println("Delete") default: println("Default") //Some code here.. } } 

Update 1: for iOS8 +

  //Create the AlertController and add Its action like button in Actionsheet let actionSheetControllerIOS8: UIAlertController = UIAlertController(title: "Please select", message: "Option to select", preferredStyle: .ActionSheet) let cancelActionButton = UIAlertAction(title: "Cancel", style: .cancel) { _ in print("Cancel") } actionSheetControllerIOS8.addAction(cancelActionButton) let saveActionButton = UIAlertAction(title: "Save", style: .default) { _ in print("Save") } actionSheetControllerIOS8.addAction(saveActionButton) let deleteActionButton = UIAlertAction(title: "Delete", style: .default) { _ in print("Delete") } actionSheetControllerIOS8.addAction(deleteActionButton) self.present(actionSheetControllerIOS8, animated: true, completion: nil) 
+70
Apr 27 '15 at 5:46
source share

Updated for Swift 4

Works on iOS 11

Some of the other answers are fine, but I ended up mixing and matching some of them to come up with this:

 @IBAction func showAlert(sender: AnyObject) { let alert = UIAlertController(title: "Title", message: "Please Select an Option", preferredStyle: .actionSheet) alert.addAction(UIAlertAction(title: "Approve", style: .default , handler:{ (UIAlertAction)in print("User click Approve button") })) alert.addAction(UIAlertAction(title: "Edit", style: .default , handler:{ (UIAlertAction)in print("User click Edit button") })) alert.addAction(UIAlertAction(title: "Delete", style: .destructive , handler:{ (UIAlertAction)in print("User click Delete button") })) alert.addAction(UIAlertAction(title: "Dismiss", style: .cancel, handler:{ (UIAlertAction)in print("User click Dismiss button") })) self.present(alert, animated: true, completion: { print("completion block") }) } 

Enjoy :)

+74
Sep 01 '16 at 9:35
source share

UIActionSheet deprecated in iOS 8.

I am using the following:

 // Create the AlertController let actionSheetController = UIAlertController(title: "Please select", message: "How you would like to utilize the app?", preferredStyle: .ActionSheet) // Create and add the Cancel action let cancelAction = UIAlertAction(title: "Cancel", style: .Cancel) { action -> Void in // Just dismiss the action sheet } actionSheetController.addAction(cancelAction) // Create and add first option action let takePictureAction = UIAlertAction(title: "Consumer", style: .Default) { action -> Void in self.performSegueWithIdentifier("segue_setup_customer", sender: self) } actionSheetController.addAction(takePictureAction) // Create and add a second option action let choosePictureAction = UIAlertAction(title: "Service provider", style: .Default) { action -> Void in self.performSegueWithIdentifier("segue_setup_provider", sender: self) } actionSheetController.addAction(choosePictureAction) // We need to provide a popover sourceView when using it on iPad actionSheetController.popoverPresentationController?.sourceView = sender as UIView // Present the AlertController self.presentViewController(actionSheetController, animated: true, completion: nil) 
+37
01 Oct '15 at 11:49
source share

Update for Swift 3:

 // Create the AlertController and add its actions like button in ActionSheet let actionSheetController = UIAlertController(title: "Please select", message: "Option to select", preferredStyle: .actionSheet) let cancelActionButton = UIAlertAction(title: "Cancel", style: .cancel) { action -> Void in print("Cancel") } actionSheetController.addAction(cancelActionButton) let saveActionButton = UIAlertAction(title: "Save", style: .default) { action -> Void in print("Save") } actionSheetController.addAction(saveActionButton) let deleteActionButton = UIAlertAction(title: "Delete", style: .default) { action -> Void in print("Delete") } actionSheetController.addAction(deleteActionButton) self.present(actionSheetController, animated: true, completion: nil) 
+28
Jan 02 '17 at 11:55 on
source share

Updated for Swift 3

  // create an actionSheet let actionSheetController: UIAlertController = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet) // create an action let firstAction: UIAlertAction = UIAlertAction(title: "First Action", style: .default) { action -> Void in print("First Action pressed") } let secondAction: UIAlertAction = UIAlertAction(title: "Second Action", style: .default) { action -> Void in print("Second Action pressed") } let cancelAction: UIAlertAction = UIAlertAction(title: "Cancel", style: .cancel) { action -> Void in } // add actions actionSheetController.addAction(firstAction) actionSheetController.addAction(secondAction) actionSheetController.addAction(cancelAction) // present an actionSheet... present(actionSheetController, animated: true, completion: nil) 

enter image description here

+16
Aug 23 '17 at 7:27
source share

Generetic Action Sheet works for Swift 4, 4.2, 5

If you like the generic version that you can call from every ViewController and in every project, try this one:

 class Alerts { static func showActionsheet(viewController: UIViewController, title: String, message: String, actions: [(String, UIAlertActionStyle)], completion: @escaping (_ index: Int) -> Void) { let alertViewController = UIAlertController(title: title, message: message, preferredStyle: .actionSheet) for (index, (title, style)) in actions.enumerated() { let alertAction = UIAlertAction(title: title, style: style) { (_) in completion(index) } alertViewController.addAction(alertAction) } viewController.present(alertViewController, animated: true, completion: nil) } } 

Call like this in your ViewController.

  var actions: [(String, UIAlertActionStyle)] = [] actions.append(("Action 1", UIAlertActionStyle.default)) actions.append(("Action 2", UIAlertActionStyle.destructive)) actions.append(("Action 3", UIAlertActionStyle.cancel)) //self = ViewController Alerts.showActionsheet(viewController: self, title: "D_My ActionTitle", message: "General Message in Action Sheet", actions: actions) { (index) in print("call action \(index)") /* results call action 0 call action 1 call action 2 */ } 

enter image description here

Attention: maybe you are surprised why I added Action 1/2/3 but got results, for example, 0,1,2. In the line for (index, (title, style)) in actions.enumerated() I get the index of actions. Arrays always begin at index 0. Thus, the completion is 0.1.2.

If you want to set an enumeration, identifier, or other identifier, I would recommend passing the object in the parameters actions .

+12
Apr 24 '18 at 9:34
source share

List of actions in iOS10 with Swift3.0. Follow this link.

  @IBAction func ShowActionSheet(_ sender: UIButton) { // Create An UIAlertController with Action Sheet let optionMenuController = UIAlertController(title: nil, message: "Choose Option from Action Sheet", preferredStyle: .actionSheet) // Create UIAlertAction for UIAlertController let addAction = UIAlertAction(title: "Add", style: .default, handler: { (alert: UIAlertAction!) -> Void in print("File has been Add") }) let saveAction = UIAlertAction(title: "Edit", style: .default, handler: { (alert: UIAlertAction!) -> Void in print("File has been Edit") }) let deleteAction = UIAlertAction(title: "Delete", style: .default, handler: { (alert: UIAlertAction!) -> Void in print("File has been Delete") }) let cancelAction = UIAlertAction(title: "Cancel", style: .cancel, handler: { (alert: UIAlertAction!) -> Void in print("Cancel") }) // Add UIAlertAction in UIAlertController optionMenuController.addAction(addAction) optionMenuController.addAction(saveAction) optionMenuController.addAction(deleteAction) optionMenuController.addAction(cancelAction) // Present UIAlertController with Action Sheet self.present(optionMenuController, animated: true, completion: nil) } 
+4
Mar 09 '17 at 6:12
source share

Old way: UIActionSheet

 let actionSheet = UIActionSheet(title: "Takes the appearance of the bottom bar if specified; otherwise, same as UIActionSheetStyleDefault.", delegate: self, cancelButtonTitle: "Cancel", destructiveButtonTitle: "Destroy", otherButtonTitles: "OK") actionSheet.actionSheetStyle = .Default actionSheet.showInView(self.view) // MARK: UIActionSheetDelegate func actionSheet(actionSheet: UIActionSheet, clickedButtonAtIndex buttonIndex: Int) { switch buttonIndex { ... } } 

New way: UIAlertController

 let alertController = UIAlertController(title: nil, message: "Takes the appearance of the bottom bar if specified; otherwise, same as UIActionSheetStyleDefault.", preferredStyle: .ActionSheet) let cancelAction = UIAlertAction(title: "Cancel", style: .Cancel) { (action) in // ... } alertController.addAction(cancelAction) let OKAction = UIAlertAction(title: "OK", style: .Default) { (action) in // ... } alertController.addAction(OKAction) let destroyAction = UIAlertAction(title: "Destroy", style: .Destructive) { (action) in println(action) } alertController.addAction(destroyAction) self.presentViewController(alertController, animated: true) { // ... } 
+2
Jul 04 '16 at 8:47
source share

Swift 3 To display a UIAlertController from a UIBarButtonItem on an iPad

  let alert = UIAlertController(title: "Title", message: "Please Select an Option", preferredStyle: .actionSheet) alert.addAction(UIAlertAction(title: "Approve", style: .default , handler:{ (UIAlertAction)in print("User click Approve button") })) alert.addAction(UIAlertAction(title: "Edit", style: .default , handler:{ (UIAlertAction)in print("User click Edit button") })) alert.addAction(UIAlertAction(title: "Delete", style: .destructive , handler:{ (UIAlertAction)in print("User click Delete button") })) alert.addAction(UIAlertAction(title: "Dismiss", style: UIAlertActionStyle.cancel, handler:{ (UIAlertAction)in print("User click Dismiss button") })) if let presenter = alert.popoverPresentationController { presenter.barButtonItem = sender } self.present(alert, animated: true, completion: { print("completion block") }) 
+2
Apr 12 '17 at 7:33
source share

swift4 (verified)

 let alertController = UIAlertController(title: "Select Photo", message: "Select atleast one photo", preferredStyle: .actionSheet) let action1 = UIAlertAction(title: "From Photo", style: .default) { (action) in print("Default is pressed.....") } let action2 = UIAlertAction(title: "Cancel", style: .cancel) { (action) in print("Cancel is pressed......") } let action3 = UIAlertAction(title: "Click new", style: .default) { (action) in print("Destructive is pressed....") } alertController.addAction(action1) alertController.addAction(action2) alertController.addAction(action3) self.present(alertController, animated: true, completion: nil) } 
+2
Mar 08 '18 at 9:26
source share

Swift:

The sample code below works on both the iPhone and iPad.

  guard let viewRect = sender as? UIView else { return } let cameraSettingsAlert = UIAlertController(title: NSLocalizedString("Please choose a course", comment: ""), message: NSLocalizedString("", comment: ""), preferredStyle: .ActionSheet) cameraSettingsAlert.modalPresentationStyle = .Popover let photoResolutionAction = UIAlertAction(title: NSLocalizedString("Photo Resolution", comment: ""), style: .Default) { action in } let cameraOrientationAction = UIAlertAction(title: NSLocalizedString("Camera Orientation", comment: ""), style: .Default) { action in } let flashModeAction = UIAlertAction(title: NSLocalizedString("Flash Mode", comment: ""), style: .Default) { action in } let timeStampOnPhotoAction = UIAlertAction(title: NSLocalizedString("Time Stamp on Photo", comment: ""), style: .Default) { action in } let cancel = UIAlertAction(title: NSLocalizedString("Cancel", comment: ""), style: .Cancel) { action in } cameraSettingsAlert.addAction(cancel) cameraSettingsAlert.addAction(cameraOrientationAction) cameraSettingsAlert.addAction(flashModeAction) cameraSettingsAlert.addAction(timeStampOnPhotoAction) cameraSettingsAlert.addAction(photoResolutionAction) if let presenter = cameraSettingsAlert.popoverPresentationController { presenter.sourceView = viewRect; presenter.sourceRect = viewRect.bounds; } presentViewController(cameraSettingsAlert, animated: true, completion: nil) 
+1
Apr 08 '16 at 9:30
source share

SWIFT 4

WORKS ON IPHONE AND IPAD BOTH. ALSO RESOLVES ROTATION

LANDSCAPE LANDSCAPE

PORTRAIT

PORTRAIT

Code (verified)

 let alert = UIAlertController() let width: Int = Int(UIScreen.main.bounds.width - 100) let viewAction = UIAlertAction(title: "View", style: .default , handler:{ (UIAlertAction)in let storyboard = UIStoryboard(name: "Main", bundle: nil) let orderDetailVC = storyboard.instantiateViewController(withIdentifier: "orderDetail") as! OrderDetailTableViewController orderDetailVC.orderId = self.myDraftOrders[indexPath.row]["id"].intValue self.navigationController?.pushViewController(orderDetailVC, animated: true) }) viewAction.setValue(appGreenColor, forKey: "titleTextColor") alert.addAction(viewAction) let modifyAction = UIAlertAction(title: "Modify", style: .default, handler:{ (UIAlertAction)in showAlert("Coming soon...") }) modifyAction.setValue(appCyanColor, forKey: "titleTextColor") alert.addAction(modifyAction) let copyAction = UIAlertAction(title: "Copy", style: .default, handler:{ (UIAlertAction)in self.copyOrder(orderId: self.myDraftOrders[indexPath.row]["id"].intValue) }) copyAction.setValue(appBlueColor, forKey: "titleTextColor") alert.addAction(copyAction) alert.addAction(UIAlertAction(title: "Delete", style: .destructive , handler:{ (UIAlertAction)in self.deleteOrder(orderId: self.myDraftOrders[indexPath.row]["id"].intValue, indexPath: indexPath) })) alert.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler:{ (UIAlertAction)in print("User click Dismiss button") })) let popover = alert.popoverPresentationController popover?.delegate = self let cellT = tableView.cellForRow(at: indexPath) popover?.sourceView = cellT popover?.sourceRect = CGRect(x: width, y: 25, width: 100, height: 50) present(alert, animated: true) 
0
Nov 13 '18 at 9:56
source share

You can use the power of the Swift protocol, I recommend this repository

https://github.com/dsngeu/SwiftActionSheet

0
Jun 17 '19 at 12:43 on
source share

You can use the following code for open ActionSheet in Swift

  let alert = UIAlertController(title: enter your title, message: "Enter your messgage. ", preferredStyle: UIAlertControllerStyle.Alert) alert.addTextFieldWithConfigurationHandler(configurationTextField) alert.addAction(UIAlertAction(title: "Close", style: UIAlertActionStyle.Cancel, handler:{ (UIAlertAction)in print("User click Cancel button") })) alert.addAction(UIAlertAction(title: "Ok", style: UIAlertActionStyle.Default, handler:{ (UIAlertAction)in print("User click Ok button") })) self.presentViewController(alert, animated: true, completion: { print("completion block") }) 
-one
Aug 20 '16 at 10:49 on
source share



All Articles