Skip to content

Instantly share code, notes, and snippets.

@danyalaytekin
Last active December 18, 2015 19:19
Show Gist options
  • Save danyalaytekin/5831704 to your computer and use it in GitHub Desktop.
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.
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