How to show Actionsheet in iPad





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







5















How can I show a UIActionsheet in iPad when I'm using my current code its giving me this error:




Your application has presented a UIAlertController (<UIAlertController: 0x7f9ec624af70>) of style UIAlertControllerStyleActionSheet. The modalPresentationStyle of a UIAlertController with this style is UIModalPresentationPopover. You must provide location information for this popover through the alert controller's popoverPresentationController. You must provide either a sourceView and sourceRect or a barButtonItem. If this information is not known when you present the alert controller, you may provide it in the UIPopoverPresentationControllerDelegate method -prepareForPopoverPresentation.




which is working totally fine in an iPhone :



let optionMenu = UIAlertController(title: nil, message: nil, preferredStyle: .ActionSheet)
let reminderAction = UIAlertAction(title: "Reminder", style: .Default, handler: {
(alert: UIAlertAction!) -> Void in }
optionMenu.addAction(reminderAction)
self.presentViewController(optionMenu, animated: true, completion: nil)


I came across some similar problems, the solution was this:



let optionMenu = UIAlertController(title: nil, message: nil, preferredStyle: .ActionSheet)
optionMenu.popoverPresentationController?.sourceView = self.view
optionMenu.popoverPresentationController?.sourceRect = self.view.bounds


but it didnt worked for me maybe because my ActionSheet's Sender is on a UItableviewCell.



I tired to set AlertController's Sourceview to tableView's Cell but its not correctly placed and sometime its partially visible this is what I tried:



optionMenu.popoverPresentationController?.sourceView = currentCell.contentView
optionMenu.popoverPresentationController?.sourceRect = currentCell.contentView.bounds


Any clue how can I fix this problem?










share|improve this question

























  • So the problem is that you cant position properly your popover controller?

    – Robert
    Aug 9 '16 at 19:52











  • yes @roher , its not proper and also AlertView is flashing while interacting to it

    – remy boys
    Aug 9 '16 at 20:36


















5















How can I show a UIActionsheet in iPad when I'm using my current code its giving me this error:




Your application has presented a UIAlertController (<UIAlertController: 0x7f9ec624af70>) of style UIAlertControllerStyleActionSheet. The modalPresentationStyle of a UIAlertController with this style is UIModalPresentationPopover. You must provide location information for this popover through the alert controller's popoverPresentationController. You must provide either a sourceView and sourceRect or a barButtonItem. If this information is not known when you present the alert controller, you may provide it in the UIPopoverPresentationControllerDelegate method -prepareForPopoverPresentation.




which is working totally fine in an iPhone :



let optionMenu = UIAlertController(title: nil, message: nil, preferredStyle: .ActionSheet)
let reminderAction = UIAlertAction(title: "Reminder", style: .Default, handler: {
(alert: UIAlertAction!) -> Void in }
optionMenu.addAction(reminderAction)
self.presentViewController(optionMenu, animated: true, completion: nil)


I came across some similar problems, the solution was this:



let optionMenu = UIAlertController(title: nil, message: nil, preferredStyle: .ActionSheet)
optionMenu.popoverPresentationController?.sourceView = self.view
optionMenu.popoverPresentationController?.sourceRect = self.view.bounds


but it didnt worked for me maybe because my ActionSheet's Sender is on a UItableviewCell.



I tired to set AlertController's Sourceview to tableView's Cell but its not correctly placed and sometime its partially visible this is what I tried:



optionMenu.popoverPresentationController?.sourceView = currentCell.contentView
optionMenu.popoverPresentationController?.sourceRect = currentCell.contentView.bounds


Any clue how can I fix this problem?










share|improve this question

























  • So the problem is that you cant position properly your popover controller?

    – Robert
    Aug 9 '16 at 19:52











  • yes @roher , its not proper and also AlertView is flashing while interacting to it

    – remy boys
    Aug 9 '16 at 20:36














5












5








5


1






How can I show a UIActionsheet in iPad when I'm using my current code its giving me this error:




Your application has presented a UIAlertController (<UIAlertController: 0x7f9ec624af70>) of style UIAlertControllerStyleActionSheet. The modalPresentationStyle of a UIAlertController with this style is UIModalPresentationPopover. You must provide location information for this popover through the alert controller's popoverPresentationController. You must provide either a sourceView and sourceRect or a barButtonItem. If this information is not known when you present the alert controller, you may provide it in the UIPopoverPresentationControllerDelegate method -prepareForPopoverPresentation.




which is working totally fine in an iPhone :



let optionMenu = UIAlertController(title: nil, message: nil, preferredStyle: .ActionSheet)
let reminderAction = UIAlertAction(title: "Reminder", style: .Default, handler: {
(alert: UIAlertAction!) -> Void in }
optionMenu.addAction(reminderAction)
self.presentViewController(optionMenu, animated: true, completion: nil)


I came across some similar problems, the solution was this:



let optionMenu = UIAlertController(title: nil, message: nil, preferredStyle: .ActionSheet)
optionMenu.popoverPresentationController?.sourceView = self.view
optionMenu.popoverPresentationController?.sourceRect = self.view.bounds


but it didnt worked for me maybe because my ActionSheet's Sender is on a UItableviewCell.



I tired to set AlertController's Sourceview to tableView's Cell but its not correctly placed and sometime its partially visible this is what I tried:



optionMenu.popoverPresentationController?.sourceView = currentCell.contentView
optionMenu.popoverPresentationController?.sourceRect = currentCell.contentView.bounds


Any clue how can I fix this problem?










share|improve this question
















How can I show a UIActionsheet in iPad when I'm using my current code its giving me this error:




Your application has presented a UIAlertController (<UIAlertController: 0x7f9ec624af70>) of style UIAlertControllerStyleActionSheet. The modalPresentationStyle of a UIAlertController with this style is UIModalPresentationPopover. You must provide location information for this popover through the alert controller's popoverPresentationController. You must provide either a sourceView and sourceRect or a barButtonItem. If this information is not known when you present the alert controller, you may provide it in the UIPopoverPresentationControllerDelegate method -prepareForPopoverPresentation.




which is working totally fine in an iPhone :



let optionMenu = UIAlertController(title: nil, message: nil, preferredStyle: .ActionSheet)
let reminderAction = UIAlertAction(title: "Reminder", style: .Default, handler: {
(alert: UIAlertAction!) -> Void in }
optionMenu.addAction(reminderAction)
self.presentViewController(optionMenu, animated: true, completion: nil)


I came across some similar problems, the solution was this:



let optionMenu = UIAlertController(title: nil, message: nil, preferredStyle: .ActionSheet)
optionMenu.popoverPresentationController?.sourceView = self.view
optionMenu.popoverPresentationController?.sourceRect = self.view.bounds


but it didnt worked for me maybe because my ActionSheet's Sender is on a UItableviewCell.



I tired to set AlertController's Sourceview to tableView's Cell but its not correctly placed and sometime its partially visible this is what I tried:



optionMenu.popoverPresentationController?.sourceView = currentCell.contentView
optionMenu.popoverPresentationController?.sourceRect = currentCell.contentView.bounds


Any clue how can I fix this problem?







ios swift ipad uialertcontroller






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Aug 9 '16 at 23:03









J F

2,74942432




2,74942432










asked Aug 9 '16 at 19:24









remy boysremy boys

1,27711746




1,27711746













  • So the problem is that you cant position properly your popover controller?

    – Robert
    Aug 9 '16 at 19:52











  • yes @roher , its not proper and also AlertView is flashing while interacting to it

    – remy boys
    Aug 9 '16 at 20:36



















  • So the problem is that you cant position properly your popover controller?

    – Robert
    Aug 9 '16 at 19:52











  • yes @roher , its not proper and also AlertView is flashing while interacting to it

    – remy boys
    Aug 9 '16 at 20:36

















So the problem is that you cant position properly your popover controller?

– Robert
Aug 9 '16 at 19:52





So the problem is that you cant position properly your popover controller?

– Robert
Aug 9 '16 at 19:52













yes @roher , its not proper and also AlertView is flashing while interacting to it

– remy boys
Aug 9 '16 at 20:36





yes @roher , its not proper and also AlertView is flashing while interacting to it

– remy boys
Aug 9 '16 at 20:36












4 Answers
4






active

oldest

votes


















7














The sample code given below works both on 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)





share|improve this answer
























  • can't we set this View (the ActivityView) to centre of Current Window (screen) because here my sender is a UIView (which disappears once tap on it to open this activityView)

    – remy boys
    Aug 10 '16 at 9:39











  • Seems to me that the key is this bit of code: presenter.sourceView = viewRect; presenter.sourceRect = viewRect.bounds;

    – David Weiss
    May 4 '17 at 21:40













  • Thanks! It works!

    – BurtK
    Jul 29 '17 at 4:07



















3














If you want to show any ActionSheet on iPad so their use popoverPresentationController to show and at iPad don't show the cancel style button of action sheet.
Use this code in Swift 3:



  @IBAction func ActionSheetShow(_ sender: UIButton) {

let actionSheet = UIAlertController(title: "Choose any option", message: "choose as you like here!", preferredStyle: .actionSheet)

actionSheet.addAction(UIAlertAction(title: "Click1", style: .cancel, handler: {
action in
print("first button")
}))
actionSheet.addAction(UIAlertAction(title: "Click2", style: .default, handler: {
action in
print("second button")
}))
actionSheet.addAction(UIAlertAction(title: "Click3", style: .destructive, handler: {
action in
print("third button")
}))
actionSheet.popoverPresentationController?.sourceView = self.view
actionSheet.popoverPresentationController?.sourceRect = sender.frame
present(actionSheet, animated: true, completion: nil)
}


Good Luck!






share|improve this answer
























  • does it work on both iphone and ipad

    – user9395315
    Jun 29 '18 at 9:16



















2














Swift 4.1 Solution:-





  1. MAK Eextension FILE UIDEviceExtension.swift and with below code :-




import Foundation
import UIKit
public extension UIDevice {

public class var isPhone: Bool {
return UIDevice.current.userInterfaceIdiom == .phone
}

public class var isPad: Bool {
return UIDevice.current.userInterfaceIdiom == .pad
}

public class var isTV: Bool {
return UIDevice.current.userInterfaceIdiom == .tv
}

public class var isCarPlay: Bool {
return UIDevice.current.userInterfaceIdiom == .carPlay
}

}




  1. Call your action Sheet on UIViewcontroller By this Separate common method :-




import Foundation
import UIKit
class Common: NSObject{
public class func showActionSheet(vc: UIViewController,sender:UIButton? = nil) {

let actionSheet = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet)

actionSheet.addAction(UIAlertAction(title: "Camera", style: .default, handler: { (alert:UIAlertAction!) -> Void in
//self.camera()
}))

actionSheet.addAction(UIAlertAction(title: "Gallery", style: .default, handler: { (alert:UIAlertAction!) -> Void in
//self.photoLibrary()
}))

actionSheet.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil))

//if iPhone
if UIDevice.isPhone {
vc.present(actionSheet, animated: true, completion: nil)
}
else {
//In iPad Change Rect to position Popover
if let btn = sender{
actionSheet.popoverPresentationController?.sourceRect = btn.frame
actionSheet.popoverPresentationController?.sourceView = vc.view

}
vc.present(actionSheet, animated: true, completion: nil)
}

}
}




  1. Use it from your UIButton Click for iPhone/iPad both :-




   @objc func btnPicImageTaped(btn:UIButton){
print("it will work for both iPhone /ipad")
Common.showActionSheet(vc: self,sender: btn)
}





share|improve this answer

































    0














    Add Following two lines before presentViewController. (Swift 3.2)



     optionMenu.popoverPresentationController?.sourceView = self.view
    optionMenu.popoverPresentationController?.sourceRect = (sender as AnyObject).frame
    present(optionMenu, animated: true, completion: nil)





    share|improve this answer


























      Your Answer






      StackExchange.ifUsing("editor", function () {
      StackExchange.using("externalEditor", function () {
      StackExchange.using("snippets", function () {
      StackExchange.snippets.init();
      });
      });
      }, "code-snippets");

      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "1"
      };
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function() {
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled) {
      StackExchange.using("snippets", function() {
      createEditor();
      });
      }
      else {
      createEditor();
      }
      });

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });














      draft saved

      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f38858878%2fhow-to-show-actionsheet-in-ipad%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      4 Answers
      4






      active

      oldest

      votes








      4 Answers
      4






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      7














      The sample code given below works both on 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)





      share|improve this answer
























      • can't we set this View (the ActivityView) to centre of Current Window (screen) because here my sender is a UIView (which disappears once tap on it to open this activityView)

        – remy boys
        Aug 10 '16 at 9:39











      • Seems to me that the key is this bit of code: presenter.sourceView = viewRect; presenter.sourceRect = viewRect.bounds;

        – David Weiss
        May 4 '17 at 21:40













      • Thanks! It works!

        – BurtK
        Jul 29 '17 at 4:07
















      7














      The sample code given below works both on 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)





      share|improve this answer
























      • can't we set this View (the ActivityView) to centre of Current Window (screen) because here my sender is a UIView (which disappears once tap on it to open this activityView)

        – remy boys
        Aug 10 '16 at 9:39











      • Seems to me that the key is this bit of code: presenter.sourceView = viewRect; presenter.sourceRect = viewRect.bounds;

        – David Weiss
        May 4 '17 at 21:40













      • Thanks! It works!

        – BurtK
        Jul 29 '17 at 4:07














      7












      7








      7







      The sample code given below works both on 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)





      share|improve this answer













      The sample code given below works both on 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)






      share|improve this answer












      share|improve this answer



      share|improve this answer










      answered Aug 10 '16 at 3:10









      Prashant GhimirePrashant Ghimire

      323215




      323215













      • can't we set this View (the ActivityView) to centre of Current Window (screen) because here my sender is a UIView (which disappears once tap on it to open this activityView)

        – remy boys
        Aug 10 '16 at 9:39











      • Seems to me that the key is this bit of code: presenter.sourceView = viewRect; presenter.sourceRect = viewRect.bounds;

        – David Weiss
        May 4 '17 at 21:40













      • Thanks! It works!

        – BurtK
        Jul 29 '17 at 4:07



















      • can't we set this View (the ActivityView) to centre of Current Window (screen) because here my sender is a UIView (which disappears once tap on it to open this activityView)

        – remy boys
        Aug 10 '16 at 9:39











      • Seems to me that the key is this bit of code: presenter.sourceView = viewRect; presenter.sourceRect = viewRect.bounds;

        – David Weiss
        May 4 '17 at 21:40













      • Thanks! It works!

        – BurtK
        Jul 29 '17 at 4:07

















      can't we set this View (the ActivityView) to centre of Current Window (screen) because here my sender is a UIView (which disappears once tap on it to open this activityView)

      – remy boys
      Aug 10 '16 at 9:39





      can't we set this View (the ActivityView) to centre of Current Window (screen) because here my sender is a UIView (which disappears once tap on it to open this activityView)

      – remy boys
      Aug 10 '16 at 9:39













      Seems to me that the key is this bit of code: presenter.sourceView = viewRect; presenter.sourceRect = viewRect.bounds;

      – David Weiss
      May 4 '17 at 21:40







      Seems to me that the key is this bit of code: presenter.sourceView = viewRect; presenter.sourceRect = viewRect.bounds;

      – David Weiss
      May 4 '17 at 21:40















      Thanks! It works!

      – BurtK
      Jul 29 '17 at 4:07





      Thanks! It works!

      – BurtK
      Jul 29 '17 at 4:07













      3














      If you want to show any ActionSheet on iPad so their use popoverPresentationController to show and at iPad don't show the cancel style button of action sheet.
      Use this code in Swift 3:



        @IBAction func ActionSheetShow(_ sender: UIButton) {

      let actionSheet = UIAlertController(title: "Choose any option", message: "choose as you like here!", preferredStyle: .actionSheet)

      actionSheet.addAction(UIAlertAction(title: "Click1", style: .cancel, handler: {
      action in
      print("first button")
      }))
      actionSheet.addAction(UIAlertAction(title: "Click2", style: .default, handler: {
      action in
      print("second button")
      }))
      actionSheet.addAction(UIAlertAction(title: "Click3", style: .destructive, handler: {
      action in
      print("third button")
      }))
      actionSheet.popoverPresentationController?.sourceView = self.view
      actionSheet.popoverPresentationController?.sourceRect = sender.frame
      present(actionSheet, animated: true, completion: nil)
      }


      Good Luck!






      share|improve this answer
























      • does it work on both iphone and ipad

        – user9395315
        Jun 29 '18 at 9:16
















      3














      If you want to show any ActionSheet on iPad so their use popoverPresentationController to show and at iPad don't show the cancel style button of action sheet.
      Use this code in Swift 3:



        @IBAction func ActionSheetShow(_ sender: UIButton) {

      let actionSheet = UIAlertController(title: "Choose any option", message: "choose as you like here!", preferredStyle: .actionSheet)

      actionSheet.addAction(UIAlertAction(title: "Click1", style: .cancel, handler: {
      action in
      print("first button")
      }))
      actionSheet.addAction(UIAlertAction(title: "Click2", style: .default, handler: {
      action in
      print("second button")
      }))
      actionSheet.addAction(UIAlertAction(title: "Click3", style: .destructive, handler: {
      action in
      print("third button")
      }))
      actionSheet.popoverPresentationController?.sourceView = self.view
      actionSheet.popoverPresentationController?.sourceRect = sender.frame
      present(actionSheet, animated: true, completion: nil)
      }


      Good Luck!






      share|improve this answer
























      • does it work on both iphone and ipad

        – user9395315
        Jun 29 '18 at 9:16














      3












      3








      3







      If you want to show any ActionSheet on iPad so their use popoverPresentationController to show and at iPad don't show the cancel style button of action sheet.
      Use this code in Swift 3:



        @IBAction func ActionSheetShow(_ sender: UIButton) {

      let actionSheet = UIAlertController(title: "Choose any option", message: "choose as you like here!", preferredStyle: .actionSheet)

      actionSheet.addAction(UIAlertAction(title: "Click1", style: .cancel, handler: {
      action in
      print("first button")
      }))
      actionSheet.addAction(UIAlertAction(title: "Click2", style: .default, handler: {
      action in
      print("second button")
      }))
      actionSheet.addAction(UIAlertAction(title: "Click3", style: .destructive, handler: {
      action in
      print("third button")
      }))
      actionSheet.popoverPresentationController?.sourceView = self.view
      actionSheet.popoverPresentationController?.sourceRect = sender.frame
      present(actionSheet, animated: true, completion: nil)
      }


      Good Luck!






      share|improve this answer













      If you want to show any ActionSheet on iPad so their use popoverPresentationController to show and at iPad don't show the cancel style button of action sheet.
      Use this code in Swift 3:



        @IBAction func ActionSheetShow(_ sender: UIButton) {

      let actionSheet = UIAlertController(title: "Choose any option", message: "choose as you like here!", preferredStyle: .actionSheet)

      actionSheet.addAction(UIAlertAction(title: "Click1", style: .cancel, handler: {
      action in
      print("first button")
      }))
      actionSheet.addAction(UIAlertAction(title: "Click2", style: .default, handler: {
      action in
      print("second button")
      }))
      actionSheet.addAction(UIAlertAction(title: "Click3", style: .destructive, handler: {
      action in
      print("third button")
      }))
      actionSheet.popoverPresentationController?.sourceView = self.view
      actionSheet.popoverPresentationController?.sourceRect = sender.frame
      present(actionSheet, animated: true, completion: nil)
      }


      Good Luck!







      share|improve this answer












      share|improve this answer



      share|improve this answer










      answered Aug 19 '17 at 10:03









      Gourav kumarGourav kumar

      411




      411













      • does it work on both iphone and ipad

        – user9395315
        Jun 29 '18 at 9:16



















      • does it work on both iphone and ipad

        – user9395315
        Jun 29 '18 at 9:16

















      does it work on both iphone and ipad

      – user9395315
      Jun 29 '18 at 9:16





      does it work on both iphone and ipad

      – user9395315
      Jun 29 '18 at 9:16











      2














      Swift 4.1 Solution:-





      1. MAK Eextension FILE UIDEviceExtension.swift and with below code :-




      import Foundation
      import UIKit
      public extension UIDevice {

      public class var isPhone: Bool {
      return UIDevice.current.userInterfaceIdiom == .phone
      }

      public class var isPad: Bool {
      return UIDevice.current.userInterfaceIdiom == .pad
      }

      public class var isTV: Bool {
      return UIDevice.current.userInterfaceIdiom == .tv
      }

      public class var isCarPlay: Bool {
      return UIDevice.current.userInterfaceIdiom == .carPlay
      }

      }




      1. Call your action Sheet on UIViewcontroller By this Separate common method :-




      import Foundation
      import UIKit
      class Common: NSObject{
      public class func showActionSheet(vc: UIViewController,sender:UIButton? = nil) {

      let actionSheet = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet)

      actionSheet.addAction(UIAlertAction(title: "Camera", style: .default, handler: { (alert:UIAlertAction!) -> Void in
      //self.camera()
      }))

      actionSheet.addAction(UIAlertAction(title: "Gallery", style: .default, handler: { (alert:UIAlertAction!) -> Void in
      //self.photoLibrary()
      }))

      actionSheet.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil))

      //if iPhone
      if UIDevice.isPhone {
      vc.present(actionSheet, animated: true, completion: nil)
      }
      else {
      //In iPad Change Rect to position Popover
      if let btn = sender{
      actionSheet.popoverPresentationController?.sourceRect = btn.frame
      actionSheet.popoverPresentationController?.sourceView = vc.view

      }
      vc.present(actionSheet, animated: true, completion: nil)
      }

      }
      }




      1. Use it from your UIButton Click for iPhone/iPad both :-




         @objc func btnPicImageTaped(btn:UIButton){
      print("it will work for both iPhone /ipad")
      Common.showActionSheet(vc: self,sender: btn)
      }





      share|improve this answer






























        2














        Swift 4.1 Solution:-





        1. MAK Eextension FILE UIDEviceExtension.swift and with below code :-




        import Foundation
        import UIKit
        public extension UIDevice {

        public class var isPhone: Bool {
        return UIDevice.current.userInterfaceIdiom == .phone
        }

        public class var isPad: Bool {
        return UIDevice.current.userInterfaceIdiom == .pad
        }

        public class var isTV: Bool {
        return UIDevice.current.userInterfaceIdiom == .tv
        }

        public class var isCarPlay: Bool {
        return UIDevice.current.userInterfaceIdiom == .carPlay
        }

        }




        1. Call your action Sheet on UIViewcontroller By this Separate common method :-




        import Foundation
        import UIKit
        class Common: NSObject{
        public class func showActionSheet(vc: UIViewController,sender:UIButton? = nil) {

        let actionSheet = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet)

        actionSheet.addAction(UIAlertAction(title: "Camera", style: .default, handler: { (alert:UIAlertAction!) -> Void in
        //self.camera()
        }))

        actionSheet.addAction(UIAlertAction(title: "Gallery", style: .default, handler: { (alert:UIAlertAction!) -> Void in
        //self.photoLibrary()
        }))

        actionSheet.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil))

        //if iPhone
        if UIDevice.isPhone {
        vc.present(actionSheet, animated: true, completion: nil)
        }
        else {
        //In iPad Change Rect to position Popover
        if let btn = sender{
        actionSheet.popoverPresentationController?.sourceRect = btn.frame
        actionSheet.popoverPresentationController?.sourceView = vc.view

        }
        vc.present(actionSheet, animated: true, completion: nil)
        }

        }
        }




        1. Use it from your UIButton Click for iPhone/iPad both :-




           @objc func btnPicImageTaped(btn:UIButton){
        print("it will work for both iPhone /ipad")
        Common.showActionSheet(vc: self,sender: btn)
        }





        share|improve this answer




























          2












          2








          2







          Swift 4.1 Solution:-





          1. MAK Eextension FILE UIDEviceExtension.swift and with below code :-




          import Foundation
          import UIKit
          public extension UIDevice {

          public class var isPhone: Bool {
          return UIDevice.current.userInterfaceIdiom == .phone
          }

          public class var isPad: Bool {
          return UIDevice.current.userInterfaceIdiom == .pad
          }

          public class var isTV: Bool {
          return UIDevice.current.userInterfaceIdiom == .tv
          }

          public class var isCarPlay: Bool {
          return UIDevice.current.userInterfaceIdiom == .carPlay
          }

          }




          1. Call your action Sheet on UIViewcontroller By this Separate common method :-




          import Foundation
          import UIKit
          class Common: NSObject{
          public class func showActionSheet(vc: UIViewController,sender:UIButton? = nil) {

          let actionSheet = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet)

          actionSheet.addAction(UIAlertAction(title: "Camera", style: .default, handler: { (alert:UIAlertAction!) -> Void in
          //self.camera()
          }))

          actionSheet.addAction(UIAlertAction(title: "Gallery", style: .default, handler: { (alert:UIAlertAction!) -> Void in
          //self.photoLibrary()
          }))

          actionSheet.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil))

          //if iPhone
          if UIDevice.isPhone {
          vc.present(actionSheet, animated: true, completion: nil)
          }
          else {
          //In iPad Change Rect to position Popover
          if let btn = sender{
          actionSheet.popoverPresentationController?.sourceRect = btn.frame
          actionSheet.popoverPresentationController?.sourceView = vc.view

          }
          vc.present(actionSheet, animated: true, completion: nil)
          }

          }
          }




          1. Use it from your UIButton Click for iPhone/iPad both :-




             @objc func btnPicImageTaped(btn:UIButton){
          print("it will work for both iPhone /ipad")
          Common.showActionSheet(vc: self,sender: btn)
          }





          share|improve this answer















          Swift 4.1 Solution:-





          1. MAK Eextension FILE UIDEviceExtension.swift and with below code :-




          import Foundation
          import UIKit
          public extension UIDevice {

          public class var isPhone: Bool {
          return UIDevice.current.userInterfaceIdiom == .phone
          }

          public class var isPad: Bool {
          return UIDevice.current.userInterfaceIdiom == .pad
          }

          public class var isTV: Bool {
          return UIDevice.current.userInterfaceIdiom == .tv
          }

          public class var isCarPlay: Bool {
          return UIDevice.current.userInterfaceIdiom == .carPlay
          }

          }




          1. Call your action Sheet on UIViewcontroller By this Separate common method :-




          import Foundation
          import UIKit
          class Common: NSObject{
          public class func showActionSheet(vc: UIViewController,sender:UIButton? = nil) {

          let actionSheet = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet)

          actionSheet.addAction(UIAlertAction(title: "Camera", style: .default, handler: { (alert:UIAlertAction!) -> Void in
          //self.camera()
          }))

          actionSheet.addAction(UIAlertAction(title: "Gallery", style: .default, handler: { (alert:UIAlertAction!) -> Void in
          //self.photoLibrary()
          }))

          actionSheet.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil))

          //if iPhone
          if UIDevice.isPhone {
          vc.present(actionSheet, animated: true, completion: nil)
          }
          else {
          //In iPad Change Rect to position Popover
          if let btn = sender{
          actionSheet.popoverPresentationController?.sourceRect = btn.frame
          actionSheet.popoverPresentationController?.sourceView = vc.view

          }
          vc.present(actionSheet, animated: true, completion: nil)
          }

          }
          }




          1. Use it from your UIButton Click for iPhone/iPad both :-




             @objc func btnPicImageTaped(btn:UIButton){
          print("it will work for both iPhone /ipad")
          Common.showActionSheet(vc: self,sender: btn)
          }






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Jan 3 at 4:56

























          answered Jan 3 at 4:47









          Abhimanyu RathoreAbhimanyu Rathore

          580512




          580512























              0














              Add Following two lines before presentViewController. (Swift 3.2)



               optionMenu.popoverPresentationController?.sourceView = self.view
              optionMenu.popoverPresentationController?.sourceRect = (sender as AnyObject).frame
              present(optionMenu, animated: true, completion: nil)





              share|improve this answer






























                0














                Add Following two lines before presentViewController. (Swift 3.2)



                 optionMenu.popoverPresentationController?.sourceView = self.view
                optionMenu.popoverPresentationController?.sourceRect = (sender as AnyObject).frame
                present(optionMenu, animated: true, completion: nil)





                share|improve this answer




























                  0












                  0








                  0







                  Add Following two lines before presentViewController. (Swift 3.2)



                   optionMenu.popoverPresentationController?.sourceView = self.view
                  optionMenu.popoverPresentationController?.sourceRect = (sender as AnyObject).frame
                  present(optionMenu, animated: true, completion: nil)





                  share|improve this answer















                  Add Following two lines before presentViewController. (Swift 3.2)



                   optionMenu.popoverPresentationController?.sourceView = self.view
                  optionMenu.popoverPresentationController?.sourceRect = (sender as AnyObject).frame
                  present(optionMenu, animated: true, completion: nil)






                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited May 2 '18 at 10:10

























                  answered Dec 2 '17 at 7:02









                  PranitPranit

                  237212




                  237212






























                      draft saved

                      draft discarded




















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f38858878%2fhow-to-show-actionsheet-in-ipad%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      MongoDB - Not Authorized To Execute Command

                      in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith

                      How to fix TextFormField cause rebuild widget in Flutter