[英]Don't dismiss uialertcontroller sheet with tap gesture outside dialog area in iOS
in my iOS swift 3.0
application, I presented UIAlertController
sheet instance over my current ViewController. 在我的iOS swift 3.0
应用程序中,我在当前的ViewController上展示了UIAlertController
表实例。 But I don't want to dismiss that sheet when I tapped on outside an area of the sheet (dimmed semi-transparent background) because I already have to cancel an action. 但是当我在表格的某个区域(变暗的半透明背景)之外点击时,我不想解散该表格,因为我已经必须取消一个动作。 Any idea? 任何想法?
I have MGSwipeTableViewCell with more button. 我有带有更多按钮的MGSwipeTableViewCell。 When User clicks on that "More" button, following code executes. 当用户单击该“更多”按钮时,将执行以下代码。
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))
}
}
for UIAlertController Type as alert 用于UIAlertController类型作为警报
you can download the sample project 您可以下载示例项目
add the gesture recognizer to alertController superview for handle the userinteraction 将手势识别器添加到alertController超级视图中以处理用户交互
self.present(alertController, animated: true, completion: {() -> Void in
alertController.view.superview?.addGestureRecognizer(UITapGestureRecognizer(target: self, action: nil)
})
on that action do nothing 在那个动作上什么也不做
update 更新
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))
})
for UIAlertController Type as actionSheet 用于UIAlertController类型为actionSheet
you can download the sample project 您可以下载示例项目
you can do this two ways 你可以用两种方法
option 1 选项1
alert.view.superview.subviews[0] isUserInteractionEnabled = false
option 2 选项2
alert.view.superview?.subviews[0].addGestureRecognizer(UITapGestureRecognizer(target: self, action: nil))
for eg 例如
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))
})
full code 完整的代码
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))
})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.