Reputation: 644
in my iOS swift 3.0
application, I presented UIAlertController
sheet instance over my current ViewController. 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. 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))
}
}
Upvotes: 0
Views: 4270
Reputation: 82766
for UIAlertController Type as alert
you can download the sample project
add the gesture recognizer to alertController superview for handle the userinteraction
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
you can download the sample project
you can do this two ways
option 1
alert.view.superview.subviews[0] isUserInteractionEnabled = false
option 2
alert.view.superview?.subviews[0].addGestureRecognizer(UITapGestureRecognizer(target: self, action: nil))
for e.g
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))
})
Upvotes: 5