엑스코드(Xcode)/Swift 4
[Swift 4] Action Sheet - UIAlertController
OK + Cancel
Swift 4
func actionSheet() { let sheet = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet) let action = UIAlertAction(title: "OK", style: .default) { (action) in // Action Code } sheet.addAction(action) let cancel = UIAlertAction(title: "Cancel", style: .cancel, handler: nil) sheet.addAction(cancel) present(sheet, animated: true, completion: nil) }
Title + message + OK + Cancel
Swift 4
func actionSheet() { let sheet = UIAlertController(title: "Action Sheet", message: "message", preferredStyle: .actionSheet) let action = UIAlertAction(title: "OK", style: .default) { (action) in // Action Code } sheet.addAction(action) let cancel = UIAlertAction(title: "Cancel", style: .cancel, handler: nil) sheet.addAction(cancel) present(sheet, animated: true, completion: nil) }
Delete + Cancel
Swift 4
func actionSheet() { let sheet = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet) let delete = UIAlertAction(title: "Delete", style: .destructive) { (delete) in // Delete Code } sheet.addAction(delete) let cancel = UIAlertAction(title: "Cancel", style: .cancel, handler: nil) sheet.addAction(cancel) present(sheet, animated: true, completion: nil) }
Twitter + Facebook + Instagram + Cancel
Swift 4
func actionSheet() { let sheet = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet) let twitter = UIAlertAction(title: "Twitter", style: .default) { (twitter) in // Twitter Code } twitter.setValue(0, forKey: "titleTextAlignment") twitter.setValue(UIImage(named: "Twitter"), forKey: "image") sheet.addAction(twitter) let facebook = UIAlertAction(title: "Facebook", style: .default) { (facebook) in // Facebook Code } facebook.setValue(0, forKey: "titleTextAlignment") facebook.setValue(UIImage(named: "Facebook"), forKey: "image") sheet.addAction(facebook) let instagram = UIAlertAction(title: "Instagram", style: .default) { (instagram) in // Instagram Code } instagram.setValue(0, forKey: "titleTextAlignment") instagram.setValue(UIImage(named: "Instagram"), forKey: "image") sheet.addAction(instagram) let cancel = UIAlertAction(title: "Cancel", style: .cancel, handler: nil) sheet.addAction(cancel) present(sheet, animated: true, completion: nil) }
titleTextAlignment (0: left, 1: center, 2: right)