Xamarin.ios当在屏幕上点击时,关闭UIAlertController ActionSheet

时间:2018-07-02 12:34:19

标签: ios xamarin xamarin.ios mvvmcross uialertcontroller

我正在创建具有ActionSheet样式的UIAlertController:

UIAlertController actionSheetAlert = UIAlertController.Create(null, null, UIAlertControllerStyle.ActionSheet);

我要为其添加一个动作:

UIAlertAction alertAction = UIAlertAction.Create("Action", UIAlertActionStyle.Default, DoSomting);
var alertImage = UIImage.FromBundle("image")
                 .ImageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal);
alertImage = ResizeImage(sortingAlertImage, 32, 32)
                 .ImageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal);
alertAction.SetValueForKey(alertImage, new NSString("image"));
alertAction.SetValueForKey(UIColor.Black, new NSString("titleTextColor"));
actionSheetAlert.AddAction(alertAction);

并显示它:

PresentViewController(_actionSheetAlert, true, null);

点击屏幕时如何关闭UIAlertController?

我可以通过添加“取消”操作来做到这一点:

var cancelAlertAction = UIAlertAction.Create("Cancel", UIAlertActionStyle.Cancel, null);
cancelAlertAction.SetValueForKey(UIColor.Black, new NSString("titleTextColor"));
actionSheetAlert.AddAction(cancelAlertAction);

但是我不想显示取消动作。

3 个答案:

答案 0 :(得分:1)

虽然侯赛因的建议在一个非常基本的设置中起作用,但是当您在警报下附加了手势识别器时,如果您有其他视图,则该建议可能会受到干扰。在这种情况下(通常也可以关闭键盘),我通常会在内容视图中添加全屏不可见按钮。

public readonly UIButton CloseButton;

public MyView()
{
    CloseButton = new UIButton(UIButtonType.System);
    CloseButton.Alpha = 0; //or CloseButton.Hidden = true;

    AddSubview(CloseButton);
}

您可以在ViewDidAppear中添加处理程序,因为无法与不可见的视图进行交互。

UIAlertController actionSheetAlert;

public override void ViewDidAppear(bool animated)
{
    contentView.CloseButton.TouchUpInside += CloseAlert;
}

void CloseAlert(object sender, EventArgs e)
{
    //Notice the missing null check because the alert should never be null here. If it is you have a problem in your code and you'll find it easily.

    actionSheetAlert.DismissViewController(true, () => { });

    //Hide the button here.
}

当您打开警报对话框时,只需使按钮可见即可。如果您想花哨的方式并使用户更加清楚,请在对话框外点击将其关闭,则可以将按钮设为黑色,并在对话框打开时将alpha设置为30%。

void OpenDialog() {
    //Create dialog, add actions etc.

    UIView.Animate(0.3, () => { CloseButton.Alpha = 0.3f});
}

如果您将取消按钮保留在警报上,那么请记住将关闭按钮也隐藏在其中,其中Hidden = true或Alpha = 0。

答案 1 :(得分:0)

您必须使用以下代码进行快速处理: 您需要为其添加点击手势。

func showAlertBtnClicked(sender: UIButton) {
    let alert = UIAlertController(title: "This is title", message: "This is message", preferredStyle: .Alert)
    self.presentViewController(alert, animated: true, completion:{
        alert.view.superview?.userInteractionEnabled = true
        alert.view.superview?.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(self.alertControllerBackgroundTapped)))
    })
}

func alertControllerBackgroundTapped()
{
    self.dismissViewControllerAnimated(true, completion: nil)
}

不能正常解除警报。 或创建自定义警报视图。

答案 2 :(得分:0)

感谢https://forums.xamarin.com/profile/LandLu找到了解决方案:

UIAlertController actionSheetAlert = UIAlertController.Create(null, null, UIAlertControllerStyle.ActionSheet);

PresentViewController(actionSheetAlert, true, () => {
UITapGestureRecognizer recognizer = new UITapGestureRecognizer((tapRecognizer) =>
{
    actionSheetAlert.DismissViewController(true, null);
});
// After testing, The first subview of the screen can be used for adding gesture to dismiss the action sheet
actionSheetAlert.View.Superview.Subviews[0].AddGestureRecognizer(recognizer);
});