Last active
December 18, 2015 19:19
-
-
Save danyalaytekin/5831704 to your computer and use it in GitHub Desktop.
Dismissable modal controller for Xamarin. By inheriting it, your presented controller can be dismissed by tapping outside its view.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using MonoTouch.UIKit; | |
namespace Danyal.Xamarin.iOS | |
{ | |
public class DismissableModalController : UIViewController | |
{ | |
protected UITapGestureRecognizer _dismissRecognizer; | |
public DismissableModalController () | |
{ | |
_dismissRecognizer = new UITapGestureRecognizer (OnTapOutside); | |
_dismissRecognizer.NumberOfTapsRequired = 1u; | |
_dismissRecognizer.CancelsTouchesInView = false; | |
} | |
public override void ViewDidAppear (bool animated) | |
{ | |
base.ViewDidAppear (animated); | |
View.Window.AddGestureRecognizer (_dismissRecognizer); | |
} | |
private void OnTapOutside(UITapGestureRecognizer recogniser) | |
{ | |
if (recogniser.State == UIGestureRecognizerState.Ended) | |
{ | |
var window = View.Window; | |
DismissViewController (true, () => window.RemoveGestureRecognizer (_dismissRecognizer)); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment