如何设置 width/height of iOS UIAlertController

How to set width/height of iOS UIAlertController

几周来我一直在努力弄清楚如何更改 UIAlertController 的高度和宽度。你显然可以做到这一点,因为我已经看到了几个 Swift 示例,例如 here。我没有使用 Swift 的经验,并且我多次尝试将代码转换为 C# 都非常令人沮丧。

简而言之,我的问题是这样的。我正在使用 ZXing 扫描条形码,并使用 UIAlertController 显示 success/failure 消息,这些消息会在屏幕上显示的所有内容上弹出。我唯一能做的就是 UIAlertController。我已经尝试使用我自己的基于 UIView 的弹出视图并使用 RG.Plugins.Popup NUGet 包以及其他一些基于 RG.Plugins.PopupNUGet 包包,没有任何效果,所以我只剩下 UIAlertController。我的代码是:

        // This code is how I get the popup to show over everything
        var window = UIApplication.SharedApplication.KeyWindow;
        var vc = window.RootViewController;

        // I add a label to display personalized messages
        UILabel label = new UILabel(frame: new CGRect(0, 75, 270, 150));
        label.Lines = 5;

        var okAlertController = UIAlertController.Create(message, messagetype, UIAlertControllerStyle.Alert);
        okAlertController.AddAction(UIAlertAction.Create("OK", UIAlertActionStyle.Default, null));

        // My attempts at changing the size
        //okAlertController.ContentSizeForViewInPopover = new CGSize(600, 1000); 
        //View.Subviews[0].Subviews[0].Subviews[0].Frame = new CGRect(0,0,500,1000);
        while (vc.PresentedViewController != null)
        {
            vc = vc.PresentedViewController;
        }
        if (showLabel )
        {

            label.Text = labelText;
            label.TextAlignment = UITextAlignment.Center;
            label.Font = UIFont.BoldSystemFontOfSize(label.Font.PointSize);
            okAlertController.View.AddSubview(label);
        }

        // I change the background color based on success or error
        if (messagetype.ToUpper() == "ERROR")
        {
            okAlertController.View.Subviews[0].Subviews[0].Subviews[0].BackgroundColor = UIColor.Red;
        }
        else if (messagetype.ToUpper() == "SUCCESS")
        {
            okAlertController.View.Subviews[0].Subviews[0].Subviews[0].BackgroundColor = UIColor.Green;
        }
        // Show the popup
        vc.PresentViewController(okAlertController, true, null);

好吧,我想通了。我在 UIAlertController 中添加了一个 NSLayoutConstraint。我的代码现在是:

        var window = UIApplication.SharedApplication.KeyWindow;
        var vc = window.RootViewController;
        UILabel label = new UILabel(frame: new CGRect(0, 75, 270, 150));
        label.Lines = 5;


        var okAlertController = UIAlertController.Create(message, messagetype, UIAlertControllerStyle.Alert);
        okAlertController.AddAction(UIAlertAction.Create("OK", UIAlertActionStyle.Default, null));
        var height = NSLayoutConstraint.Create(okAlertController.View, NSLayoutAttribute.Height, NSLayoutRelation.Equal, null, NSLayoutAttribute.NoAttribute, 1.0f, (nfloat)(vc.View.Frame.Height * 0.50));
        okAlertController.View.AddConstraint(height);
        while (vc.PresentedViewController != null)
        {
            vc = vc.PresentedViewController;
        }
        if (showLabel )
        {

            label.Text = labelText;
            label.TextAlignment = UITextAlignment.Center;
            label.Font = UIFont.BoldSystemFontOfSize(label.Font.PointSize);
            okAlertController.View.AddSubview(label);
        }
        if (messagetype.ToUpper() == "ERROR")
        {
            okAlertController.View.Subviews[0].Subviews[0].Subviews[0].BackgroundColor = UIColor.Red;
        }
        else if (messagetype.ToUpper() == "SUCCESS")
        {
            okAlertController.View.Subviews[0].Subviews[0].Subviews[0].BackgroundColor = UIColor.Green;
        }
        vc.PresentViewController(okAlertController, true, null);

我现在达到了我需要的高度。