【问题标题】:Action Sheet is not working in ipad ios 13.6操作表在 ipad ios 13.6 中不起作用
【发布时间】:2020-07-22 05:57:05
【问题描述】:

我的应用程序将 UIAlertController 用于 ActionSheet 和 Alert。它在 iPad 中的 iOS 13.4 上运行良好,但如果我从我的 iOS 13.6 设备运行代码,则在 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)

【问题讨论】:

  • " popoverPresentationController.sourceView = sender" 在这一行中,什么是“sender”?是“发件人”是 UIButton
  • @Ashutosh kumar Mishra 先生,是的,它的 UIButton
  • Hey Pawan,我已经在模拟器中运行了你的代码(设备:- iPhone 11 Pro 13.6),它工作正常。
  • @Ashutosh kumar Mishra 先生,它也不能在 Ipad 12.9(第 4 代)ios 13.6 上运行
  • 嗨 Pawan,现在我在 iPad pro(12.9- 第 4 代)上使用了你的代码。再次它正常工作。

标签: ios swift iphone alert uialertcontroller


【解决方案1】:

我已经在 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)

}

【讨论】:

    【解决方案2】:

    试试下面:

     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)
      }
     
    

    【讨论】:

      【解决方案3】:
      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)
      

      【讨论】:

      • 还请简要解释一下代码在做什么。
      猜你喜欢
      • 2020-11-09
      • 1970-01-01
      • 2012-10-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多