How to display actionsheet using swift
Swift - Displaying Actionsheet for Ipad and Iphone A simple tutorial on making an alert view of type action sheet that works for iphone an...
https://www.czetsuyatech.com/2015/07/xcode-displaying-actionsheet.html
Swift - Displaying Actionsheet for Ipad and Iphone
A simple tutorial on making an alert view of type action sheet that works for iphone and ipad.2.) Inside the button's action, paste the following lines of code:
let optionMenu = UIAlertController(title: "Choose Your Option", message: nil, preferredStyle: UIAlertControllerStyle.ActionSheet) let option1 = UIAlertAction(title: "Option 1", style: .Default, handler: { (alert: UIAlertAction!) -> Void in println("Option 1") }) let option2 = UIAlertAction(title: "Option ", style: .Default, handler: { (alert: UIAlertAction!) -> Void in println("Option 2") }) let cancelAction = UIAlertAction(title: "Cancel", style: .Cancel, handler: { (alert: UIAlertAction!) -> Void in println("Cancelled") }) optionMenu.addAction(option1) optionMenu.addAction(option2) optionMenu.addAction(cancelAction) if ( UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiom.Pad ) { if let currentPopoverpresentioncontroller = optionMenu.popoverPresentationController{ currentPopoverpresentioncontroller.sourceView = btn_button currentPopoverpresentioncontroller.sourceRect = btn_button.bounds; currentPopoverpresentioncontroller.permittedArrowDirections = UIPopoverArrowDirection.Up; self.presentViewController(optionMenu, animated: true, completion: nil) } }else{ self.presentViewController(optionMenu, animated: true, completion: nil) }The code is pretty straight forward:
-First we declare a UIAlertController where we can put our buttons.
-Second we created the buttons (Option1, Option2, and Cancel) and then we add the buttons inside the alert controller.
-Last we use the UI_USER_INTERFACE_IDIOM to determine if the device/emulator is an ipad or an iphone and do the necessary presentation for each particular device.
2 comments
Hi,
Big thanks for the tutorial ! How do you make the "options" to segue to another view controller ?
Thanks,
Works! You can leave out the if part, works regardless if iPhone or iPad :
if ( UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiom.Pad )
Post a Comment