UIView on top of my game on IOS? iPhone Camera?

Hello everyone.

I’m trying to make a UIView to draw a simple rectangle on the top of my game, but is that even possible? The goal is to replace this with rectangle with an UIImage of what is on the iPhone Camera, but i can’t even draw a simple white rectangle.

Did any of you guys did something like this?
Is that the best approach to take a photo on a monogame ported game?

Thank you all guys.

Get the UIViewController using:

var viewController = (UiViewController)game.Services.GetService(typeof(UIViewController);

And the View with:

var view  = viewController.View;

Then you should be able to use add subview and the normal stuff. When using native UI it will lower the performance, so make sure to remove it when you are done.

Hello dave!
And that’s what’s frustrating, every time I read something about that topic I always get a simple solution that doesn’t work for me. And since I’ve never done anything using UIView I am super lost in this.
So let me post some code.

This is my UIView Class that I am trying to draw on top of my game:

public class myUiView : UIView
        {
            public myUiView () : base() { }
   
    
            // rect changes depending on if the whole view is being redrawn, or just a section
            public override void Draw (RectangleF rect)
            {
                //Console.WriteLine (“Draw() Called”);
                base.Draw (rect);
    
                using (CGContext context = UIGraphics.GetCurrentContext ()) {
    
                    // fill the background with white
                    // set fill color
                    UIColor.White.SetFill ();
                    //context.SetRGBFillColor (1, 1, 1, 1f);
                    // paint
                    context.FillRect (rect);
    
                    // draw a rectangle using stroke rect
                    UIColor.Blue.SetStroke ();
                    context.StrokeRect (new RectangleF (10, 10, 200, 100));
    
                    // draw a rectangle using a path
                    context.BeginPath ();
                    context.MoveTo (220, 10);
                    context.AddLineToPoint (420, 10);
                    context.AddLineToPoint (420, 110);
                    context.AddLineToPoint (220, 110);
                    context.ClosePath ();
                    UIColor.DarkGray.SetFill ();
                    context.DrawPath (CGPathDrawingMode.FillStroke);
    
                    // draw a rectangle using a path
                    CGPath rectPath = new CGPath ();
                    rectPath.AddRect (new RectangleF (new PointF (430, 10), new SizeF (200, 100)));
                    context.AddPath (rectPath);
                    context.DrawPath (CGPathDrawingMode.Stroke);
    
                }
            }

It is basically the simplest UIView class from the Drawing sample on the xamarin sambles. I took one from there since I didn’t really know how exactly how to make one of those.

And after that I create a static reference to the RootUIController using the code that you suggested and called the draw function in the middle of a spritebatch call.
But it simply does not show anything all I got is the image that I am drawing on the spritebatch. At this point I started to think that something went wrong or that this code doesn’t work anymore, so I researched a little bit more and I got this code here:

UIApplication.SharedApplication.Windows[0].RootViewController.View.Addsubview(uiView);

But still no white rectangles on my screen. But hey! There are still some more codes that I can try:

UIApplication.SharedApplication.Windows[0].RootViewController.MakeKeyAndVisible();

or even trying to add on Windows[1], but nothing seems to work.

So am I doing something wrong?
Could you pleeeease help me out on this?

Do the simplest test, just create a UIView, set the background color of it and add it. (Instead of having a custom draw function)

If you try do some normal iPhone UI programming and you should be able to learn what you are doing wrong.

I haven’t don’t this myself yet, so I’m not really sure.