繁体   English   中英

操作表在 ipad ios 13.6 中不起作用

[英]Action Sheet is not working in ipad ios 13.6

我的应用程序将 UIAlertController 用于 ActionSheet 和 Alert。 It works fine on iOS 13.4 in iPad, but if I run the code from my iOS 13.6 device, this is not working properly in iPad 12.9 ios 13.6.

    let alert = UIAlertController(title: "Select Image", message: nil, preferredStyle: 
    UIAlertController.Style.actionSheet)
       alert.addAction(UIAlertAction(title: "Camera", style: UIAlertAction.Style.default, 
    handler: { (res) in
        self.btnClickedCamera(tag:2)
    }))
    alert.addAction(UIAlertAction(title: "Gallery", style: UIAlertAction.Style.default, 
    handler: { (res) in
        self.btnClickedGallery(tag:2)
    }))
    alert.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: { (res) in
        
    }))
    if let popoverPresentationController = alert.popoverPresentationController {
        popoverPresentationController.sourceView = sender
        popoverPresentationController.sourceRect = sender.bounds
    }
    self.present(alert, animated: true, completion: nil)

试试下面:

 func showAlert(vc: UIViewController) {
      let alert = UIAlertController(title: "Select Image", message: nil, preferredStyle: .actionSheet)
      alert.addAction(UIAlertAction(title: "Camera", style: .default, handler: { (res) in
         //TODO: your action
      }))
      alert.addAction(UIAlertAction(title: "Gallery", style: .default, handler: { (res) in
         //TODO: your action
      }))
      if let popoverController = alert.popoverPresentationController {
          popoverController.sourceView = vc.view
          popoverController.sourceRect = CGRect(x: vc.view.bounds.midX, y: vc.view.bounds.midY, width: 0, height: 0)
          popoverController.permittedArrowDirections = []
      }
      let cancelAction = UIAlertAction(title: "Cancel", style: .cancel) { (action) in
          alert.dismiss(animated: true, completion: nil)
      }
      vc.present(alert, animated: true, completion: nil)
  }
 

我已经在 iPhone(设备:- iPhone 11 Pro 13.6)和 iPad(iPad pro(12.9- 第 4 代)上尝试了您的代码,并且可以正常工作。但是如果您说,我已经更改了一些弹出框,请使用以下代码:-

   let alert = UIAlertController(title: "Select Image", message: nil, preferredStyle:
    UIAlertController.Style.actionSheet)
       alert.addAction(UIAlertAction(title: "Camera", style: UIAlertAction.Style.default,
    handler: { (res) in
        self.btnClickedCamera(tag:2)
    }))
    alert.addAction(UIAlertAction(title: "Gallery", style: UIAlertAction.Style.default,
    handler: { (res) in
       self.btnClickedGallery(tag:2)
    }))
    alert.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: { (res) in

    }))
    if let popoverPresentationController = alert.popoverPresentationController {

        popoverPresentationController.sourceRect = sender.frame
        popoverPresentationController.sourceView = self.view

    }
    self.present(alert, animated: true, completion: nil)

}
let alert = UIAlertController(title: "Select Image", message: nil, preferredStyle: 
    UIAlertController.Style.actionSheet)
       alert.addAction(UIAlertAction(title: "Camera", style: UIAlertAction.Style.default, 
    handler: { (res) in
        self.btnClickedCamera(tag:2)
    }))
    alert.addAction(UIAlertAction(title: "Gallery", style: UIAlertAction.Style.default, 
    handler: { (res) in
        self.btnClickedGallery(tag:2)
    }))
    alert.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: { (res) in
        
    }))
  if UIDevice.current.userInterfaceIdiom == .pad {
               if let popup = alert.popoverPresentationController {
                   popup.sourceView = self.view
                   popup.sourceRect = CGRect(x: self.view.frame.size.width / 2, y: self.view.frame.size.height / 4, width: 0, height: 0)
               }
           }
    }
    self.present(alert, animated: true, completion: nil)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM