我正在尝试将UITapGestureRecognizer附加到UIAlertController的视图,但是识别事件永远不会触发。
我正在Xamarin中对此进行编码,但我觉得该问题也适用于本机代码。
InvokeOnMainThread(() =>
var alert = new UIAlertController();
alert.Title = "My Title";
alert.Message = "My Message";
UITapGestureRecognizer tapGestureRecognizer = new
UITapGestureRecognizer((gesture) =>
{
//I never get here
});
alert.View.AddGestureRecognizer(tapGestureRecognizer);
alert.View.UserInteractionEnabled = true;
this.PresentViewController(alert, true, null);
});
理想情况下,我想在用户触摸弹出窗口时关闭警报,但是我似乎无法检测到手势。
在尝试显示警报之前和之后,我都尝试添加识别器。
答案 0 :(得分:0)
解决方案:
要通过单击背景视图关闭UIAlertController
,可以在弹出tapGestureRecognizer
时将UIAlertController
添加到屏幕上的最后一个视图,检查以下代码:
public partial class ViewController : UIViewController
{
public ViewController (IntPtr handle) : base (handle)
{
}
public override void ViewDidLoad ()
{
base.ViewDidLoad ();
// Perform any additional setup after loading the view, typically from a nib.
var alert = new UIAlertController();
alert.Title = "My Title";
alert.Message = "My Message";
UIAlertAction ac1 = UIAlertAction.Create("123",UIAlertActionStyle.Cancel,null);
alert.AddAction(ac1);
this.PresentViewController(alert, true, addGesOnBackGround);
}
public void addGesOnBackGround() {
UIView backView = new UIView();
Array arrayViews = UIApplication.SharedApplication.KeyWindow.Subviews;
if (arrayViews.Length >0)
{
backView = arrayViews.GetValue(arrayViews.Length-1) as UIView;
}
UITapGestureRecognizer tapGestureRecognizer = new
UITapGestureRecognizer((gesture) =>
{
//I never get here
this.DismissViewControllerAsync(true);
});
backView.AddGestureRecognizer(tapGestureRecognizer);
backView.UserInteractionEnabled = true;
}
}