Home > Software design >  How to set width/height of iOS UIAlertController
How to set width/height of iOS UIAlertController

Time:03-14

I've been trying to figure out how to change the height and width of the UIAlertControllerfor a couple of weeks now. You apparently can do it as I've seen several Swift examples, for example here. I have no experience with Swift and my numerous attempts at converting the code to C# have been extremely frustrating.

My problem in a nutshell is this. I'm using ZXing to scan barcodes and I use the UIAlertController to display success/failure messages that popup over everything displayed on the screen. The only thing that I can get to do that is UIAlertController. I've tried using my own popup view based on UIView and using the RG.Plugins.Popup NUGet package as well as number of other NUGet packages based on the RG.Plugins.Popup package, nothing works and so I am left with UIAlertController. My code is:

        // 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);

CodePudding user response:

Well, I was able to figure it out. I added a NSLayoutConstraint to the UIAlertController. My code is now:

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

I now get the height I need.

  • Related