【问题标题】:Xamarin.ios Close UIAlertController ActionSheet when tap on screenXamarin.ios 点击屏幕时关闭 UIAlertController ActionSheet
【发布时间】:2018-07-02 12:34:19
【问题描述】:

我正在创建一个带有 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);

但我不想显示取消操作。

【问题讨论】:

    标签: ios xamarin xamarin.ios mvvmcross uialertcontroller


    【解决方案1】:

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

    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。

    【讨论】:

      【解决方案2】:

      你必须使用下面的代码来快速: 您需要为此添加点击手势。

      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)
      }
      

      无法正常解除警报。 或创建自定义警报视图。

      【讨论】:

      • 但这并不快@Hussain Chhatriwala
      • 我只需要传达在父视图上添加点击手势,这可以帮助您在点击时关闭它。
      【解决方案3】:

      感谢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);
      });
      

      【讨论】:

        猜你喜欢
        • 2015-07-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-01-15
        • 1970-01-01
        • 2014-10-17
        • 1970-01-01
        • 2016-04-23
        相关资源
        最近更新 更多