不要在 iOS 中的对话框区域外使用点击手势关闭 uialertcontroller 表

app*_*y21 0 ios swift swift3

在我的 iOSswift 3.0应用程序中,我UIAlertController在当前的 ViewController 上显示了工作表实例。但是,当我在工作表的某个区域(变暗的半透明背景)之外点击时,我不想关闭该工作表,因为我已经必须取消一个操作。任何的想法?

我有带有更多按钮的 MGSwipeTableViewCell。当用户单击“更多”按钮时,将执行以下代码。

func onClickMore(for vmCell: VmCell) {
    let sheet = UIAlertController(title: vmCell.vmItem?.vmNameWithoutIp, message: vmCell.vmItem?.ipAddress, preferredStyle: .actionSheet)

    sheet.addAction(UIAlertAction(title: "Create Ticket", style: .default) { (action: UIAlertAction) in

    })

    sheet.addAction(UIAlertAction(title: "Start VM", style: .default) { (action: UIAlertAction) in

    })

    sheet.addAction(UIAlertAction(title: "Restart VM", style: .default) { (action: UIAlertAction) in

    })

    sheet.addAction(UIAlertAction(title: "Stop VM", style: .destructive) { (action: UIAlertAction) in

    })

    sheet.addAction(UIAlertAction(title: "Cancel", style: .cancel) { (action: UIAlertAction) in

    })

    present(sheet, animated: true) { 
        sheet.view.superview?.addGestureRecognizer(UITapGestureRecognizer(target: self, action: nil))
    }
}
Run Code Online (Sandbox Code Playgroud)

Anb*_*hik 5

对于 UIAlertController 类型为警报

您可以下载示例项目

将手势识别器添加到 alertController 超级视图以处理用户交互

self.present(alertController, animated: true, completion: {() -> Void in
 alertController.view.superview?.addGestureRecognizer(UITapGestureRecognizer(target: self, action: nil)
})
Run Code Online (Sandbox Code Playgroud)

在那个动作上什么都不做

更新

let alertController = UIAlertController(title: "Do something", message: "With this", preferredStyle: .alert)
    alertController.addAction(UIAlertAction(title: "Done", style: .default) { action in
        // perhaps use action.title here
    })

    self.present(alertController, animated: true, completion: {() -> Void in


        alertController.view.superview?.addGestureRecognizer(UITapGestureRecognizer(target: self, action: nil))
    })
Run Code Online (Sandbox Code Playgroud)

对于 UIAlertController 类型为 actionSheet

您可以下载示例项目

你可以通过两种方式做到这一点

选项1

 alert.view.superview.subviews[0] isUserInteractionEnabled = false
Run Code Online (Sandbox Code Playgroud)

选项 2

   alert.view.superview?.subviews[0].addGestureRecognizer(UITapGestureRecognizer(target: self, action: nil))
Run Code Online (Sandbox Code Playgroud)

例如

   self.present(sheet, animated: true, completion: {() -> Void in
    //    sheet.view.superview?.subviews[0].isUserInteractionEnabled = false;
      sheet.view.superview?.subviews[0].addGestureRecognizer(UITapGestureRecognizer(target: self, action: nil))

    })
Run Code Online (Sandbox Code Playgroud)

完整代码

  let sheet = UIAlertController(title: "karthik", message: "check with", preferredStyle: .actionSheet)

    sheet.addAction(UIAlertAction(title: "Create Ticket", style: .default) { (action: UIAlertAction) in

    })

    sheet.addAction(UIAlertAction(title: "Start VM", style: .default) { (action: UIAlertAction) in

    })

    sheet.addAction(UIAlertAction(title: "Restart VM", style: .default) { (action: UIAlertAction) in

    })

    sheet.addAction(UIAlertAction(title: "Stop VM", style: .destructive) { (action: UIAlertAction) in

    })

    sheet.addAction(UIAlertAction(title: "Cancel", style: .cancel) { (action: UIAlertAction) in

    })



    self.present(sheet, animated: true, completion: {() -> Void in
    //    sheet.view.superview?.subviews[0].isUserInteractionEnabled = false;
      sheet.view.superview?.subviews[0].addGestureRecognizer(UITapGestureRecognizer(target: self, action: nil))

    })
Run Code Online (Sandbox Code Playgroud)

  • @MidhunMP - 好的,我的兄弟,但是 UIalertcontroller,没有公共 API,为了直接处理这个,我们需要采用视图层次结构,但在未来苹果改变层次结构,我肯定会更新我的答案 (2认同)
  • @Anbu.Karthik:还有其他几种方法,但不是完美的方法。1) 如果它在 iPad 中,您可以使用 popover 演示控制器委托来禁用关闭。2) 在 iPhone 中,如果您没有指定要取消的动作样式,则单击外部时不会关闭动作表,在这种情况下,您可以将样式设置为默认值。 (2认同)