【问题标题】:Pop over doesn't point over the button弹出窗口不指向按钮
【发布时间】:2015-03-05 13:39:44
【问题描述】:

我有一个与 iPhone 和 iPad 布局兼容的应用程序。对于 iPhone 布局,我为 iPad 创建了 Action Sheet 和 Pop over。问题是弹出的箭头没有指向我点击的按钮。下面是我的代码....

let actionSheet = UIAlertController(title: "Choose an option",
            message: "Message",
            preferredStyle: .ActionSheet)
...

if UIDevice.currentDevice().userInterfaceIdiom == UIUserInterfaceIdiom.Pad
{
     // for iPad
     actionSheet.popoverPresentationController?.sourceView = self.view
     actionSheet.popoverPresentationController?.sourceRect = self.view.bounds;
     actionSheet.popoverPresentationController?.permittedArrowDirections = UIPopoverArrowDirection.allZeros;
}

self.presentViewController(actionSheet, animated: true, completion: nil)

【问题讨论】:

    标签: ios ipad swift popover


    【解决方案1】:

    sourceViewsourceRect 设置为buttonbutton.bounds
    您可以根据视图的布局选择 allowedArrowDirections。

    actionSheet.popoverPresentationController?.sourceView = button
    actionSheet.popoverPresentationController?.sourceRect = button.bounds;
    actionSheet.popoverPresentationController?.permittedArrowDirections = UIPopoverArrowDirection.Left;
    

    如果按钮是 BarButtonItem,请使用此代码。

    actionSheet.popoverPresentationController?.barButtonItem = button
    actionSheet.popoverPresentationController?.permittedArrowDirections = UIPopoverArrowDirection.Up;
    

    【讨论】:

    • 好吧,那也没用。该按钮实际上是一个UIBarButtonItem。我已将函数更改为 @IBAction func userOptions(button: UIBarButtonItem) 并引发错误:[UIBarButtonItem bounds]: unrecognized selector sent
    • 检查我修改后的答案@SrujanSimha
    • sourceView 和 sourceRect 有什么区别?
    • 使用bounds 而不是frame 是我想要的。谢谢!
    【解决方案2】:

    对我来说,使用发送器并转换为 UIView。

    alertController.popoverPresentationController?.sourceView = sender as! UIView
    alertController.popoverPresentationController?.sourceRect = sender.bounds
    
    alertController.popoverPresentationController?.permittedArrowDirections = UIPopoverArrowDirection.Up
    

    【讨论】:

      【解决方案3】:

      SWIFT 3

      当我的按钮是 UIBarButtonItem 时,这对我有用:

      if UIDevice.current.userInterfaceIdiom == .pad {
      
          if controller.responds(to: "popoverPresentationController") {
              controller.popoverPresentationController?.barButtonItem = YourUIBarButtonName
          }
      
      }
      

      下面的完整代码sn-p:

      func presentActivitySheet() {
      
          let controller = UIActivityViewController(activityItems: [document.fileURL], applicationActivities: nil)
      
              if UIDevice.current.userInterfaceIdiom == .pad {
      
                  if controller.responds(to: "popoverPresentationController") {
                  controller.popoverPresentationController?.barButtonItem = YourUIBarButtonName
                  }
      
              }
      
          present(controller, animated: true, completion: nil)
      }
      

      【讨论】:

        猜你喜欢
        • 2014-10-17
        • 2011-01-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-10-16
        相关资源
        最近更新 更多