gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 10:14:26 26 4
gpt4 key购买 nike

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

我有带有更多按钮的 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))
}
}

最佳答案

对于 UIAlertController Type as alert

您可以下载 sample project

将手势识别器添加到 alertController superview 以处理用户交互

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

在那个 Action 上什么都不做

更新

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

对于 UIAlertController Type as actionSheet

您可以下载 sample project

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

选项 1

 alert.view.superview.subviews[0] isUserInteractionEnabled = false

选项 2

   alert.view.superview?.subviews[0].addGestureRecognizer(UITapGestureRecognizer(target: self, action: nil))

例如

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

})

完整代码

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

})

关于ios - 不要在 iOS 的对话框区域外用点击手势关闭 uialertcontroller 表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43199275/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com