title: Adding UIImageView to UIAlertController
body: <p>Goal: add an image above an <code>UIAlertController's</code> Title label by subclassing <code>UIAlertController</code> and adding new line characters, <code>\n</code>, to the title string to make space for the <code>UIImageView</code></p>
  
  <p>Desire
  <a href="https://i.stack.imgur.com/Ee67h.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Ee67h.jpg" alt="goal image"></a>
  Current
  <a href="https://i.stack.imgur.com/9UOHw.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/9UOHw.jpg" alt="current image"></a></p>
  
  <p>As one can see, able to add the image to the <code>UIAlertController</code> successfully but the image is not being spacing/placed above the Title. It appears to be adding to the center of the alert. How to space the image correctly above the UIAlertController title?</p>
  
  <p>Current code:    </p>
  
  <pre><code>namespace XamarinFormsApp1.Extensions
  {
      public class AlertController : UIAlertController
      {
          private string originalTitle;
          private string spaceAdjustedTitle;
          private UIImageView imageView = null;
          private CoreGraphics.CGSize previousImgViewSize 
              = CoreGraphics.CGSize.Empty;
          public override UIAlertControllerStyle PreferredStyle
          {
              get
              {
                  return UIAlertControllerStyle.Alert;
              }
          }
  
      public override string Title
      {
          get
          {
              return originalTitle;
          }
          set
          {
              if (Title != spaceAdjustedTitle ||
                  string.IsNullOrEmpty(Title) || 
                  string.IsNullOrEmpty(spaceAdjustedTitle))
              {
                  originalTitle = value;
              }
          }
      }
  
      public void setTitleImage(UIImage image)
      {
          if (this.imageView == null)
          {
              UIImageView imageView = new UIImageView(image);
              this.View.AddSubview(imageView);
              this.imageView = imageView;
              return;
          }
          imageView.Image = image;
      }
  
      public override void ViewDidLayoutSubviews()
      {
          if (imageView == null)
          {
              base.ViewDidLayoutSubviews();
              return;
          }
          //  Adjust title if image size has changed
          if (previousImgViewSize != imageView.Bounds.Size)
          {
              previousImgViewSize = imageView.Bounds.Size;
              adjustTitle(imageView);
          }
          //  Position `imageView`
          var linesCount = newLinesCount(imageView);
          var padding = Constants.Padding(PreferredStyle);
          var x = View.Bounds.Width / 2.0;
          var y = padding + linesCount * lineHeight / 2.0;
          CoreGraphics.CGPoint cgPoint = imageView.Center;
          cgPoint.X = (nfloat)x;
          cgPoint.Y = (nfloat)y;
          imageView.Center = cgPoint;
          base.ViewDidLayoutSubviews();
      }
  
      private void adjustTitle(UIImageView imageView)
      {
          var linesCount = (int)newLinesCount(imageView);
          var lines = Enumerable
              .Range(1, linesCount)
              .Select(i =&gt; "\n")
              .Aggregate((c, n) =&gt; $"{c}{n}");
          spaceAdjustedTitle = lines + (originalTitle ?? "");
          Title = spaceAdjustedTitle;
      }
  
      private double newLinesCount(UIImageView imageView)
      {
          return Math.Ceiling(
              imageView.Bounds.Height / lineHeight);
      }
  
      private float lineHeight
      {
          get
          {
              UIFontTextStyle style = this.PreferredStyle
                  == UIAlertControllerStyle.Alert 
                  ? UIFontTextStyle.Headline 
                  : UIFontTextStyle.Callout;
              return (float)UIFont
                  .GetPreferredFontForTextStyle(style)
                  .PointSize;
          }
      }
  
      struct Constants
      {
          static float paddingAlert = 22;
          static float paddingSheet = 11;
          public static float Padding(UIAlertControllerStyle style)
          {
              return style == UIAlertControllerStyle.Alert 
                  ? Constants.paddingAlert 
                  : Constants.paddingSheet;
          }
      }
  }
  }
  </code></pre>
  
  <p>Note: Credit to @stringCode for image and <code>swift</code> solution, <a href="https://stackoverflow.com/questions/37848224/how-to-add-uiimageview-in-uialertcontroller">see</a>.</p>