How can I display an image on the entire device screen with UIImageView on iPhone/iPad?

Kim Strasser 1,321 Reputation points
2021-12-06T23:53:46.097+00:00

I want to display an image on the entire device screen. The image needs to be displayed until I execute LogoView.RemoveFromSuperview();.

stackoverflow ios-fit-image-to-screen
I use the code from stackoverflow but it's not working because I don't know what I should use for my View.
I get an error message in Program.cs if I use View:

Error CS0103: The name 'View' does not exist in the current context

Program.cs:

using System;  
using Foundation;  
using UIKit;  
  
namespace MyProject  
{  
[Register("AppDelegate")]  
class Program : UIApplicationDelegate  
{  
    private UIImageView LogoImage;  
  
    public override void FinishedLaunching(UIApplication app)  
    {  
        global::Xamarin.Forms.Forms.Init();  
        LogoImage = new UIImageView(UIImage.FromFile("Launchscreenlogo.png"));  
        LogoImage.Tag = 1234;  
        LogoImage.Frame = View.Frame;  
        View.AddSubview(LogoImage);  
        View.BringSubviewToFront(LogoImage);  
    }  
  
    private void G_RemoveLogo(object sender, System.EventArgs e)  
    {  
        //remove image  
        var LogoView = View.ViewWithTag(1234);  
        if (null != LogoView)  
            LogoView.RemoveFromSuperview();  
    }  
}  
}  

How can I display an image on the entire device screen with UIImageView on iPhone/iPad?
Is it possible to use my image from Assets.xcassets(launch.png, ******@2x.png, ******@3x.png) in UIImageView?

155442-bildschirmfoto-2021-12-07-um-004603.png

Developer technologies | .NET | Xamarin
0 comments No comments
{count} votes

Accepted answer
  1. Wenyan Zhang (Shanghai Wicresoft Co,.Ltd.) 36,436 Reputation points Microsoft External Staff
    2021-12-07T08:34:42.82+00:00

    Hello,

    Welcome to our Microsoft Q&A platform!

    You got this error because there is no view in AppDelegate, I add the RootViewController and add this LogoImage into its View, you could refer to the following code:

    public override bool FinishedLaunching(UIApplication app, NSDictionary options)  
        {  
            global::Xamarin.Forms.Forms.Init();  
            LoadApplication(new App());  
        Window = new UIWindow(UIScreen.MainScreen.Bounds);  
        var MainViewController = new UIViewController();  
        Window.RootViewController = MainViewController;  
        Window.MakeKeyAndVisible();  
    
        MainViewController.View.BackgroundColor = UIColor.White;  
        LogoImage = new UIImageView(UIImage.FromBundle("Launchscreenlogo"));// load the Assets.xcassets image  
        LogoImage.ContentMode = UIViewContentMode.ScaleAspectFill;  
        LogoImage.Tag = 1234;  
        LogoImage.Frame = MainViewController.View.Frame;  
        MainViewController.View.AddSubview(LogoImage);  
        MainViewController.View.BringSubviewToFront(LogoImage);  
        return true;    
    }  
    

    You could also create a new controller to load a imageview and add a button to test, right click the iOS project ->new Item -> UIViewController(Apple) ->named( my class name is MyViewController) , then refer to the following code:

     private UIImageView LogoImage;  
            public override void ViewDidLoad()  
            {  
                View = new UniversalView();  
      
                base.ViewDidLoad();  
                View.BackgroundColor = UIColor.White;  
                LogoImage = new UIImageView(UIImage.FromBundle("Launchscreenlogo"));  
                LogoImage.ContentMode = UIViewContentMode.ScaleAspectFill;  
                LogoImage.Tag = 1234;  
                LogoImage.Frame = new CoreGraphics.CGRect(0,0,UIScreen.MainScreen.Bounds.Size.Width, UIScreen.MainScreen.Bounds.Size.Height);// important  
                View.AddSubview(LogoImage);  
                  
      
                UIButton mybutton = new UIButton(new CoreGraphics.CGRect(0, 100, 100, 40));  
                mybutton.SetTitle("click to remove", UIControlState.Normal);// add this button to test  
                mybutton.TouchUpInside += (sender, e) => {  
                    G_RemoveLogo(sender, e);  
                };  
                View.AddSubview(mybutton);  
            }  
            private void G_RemoveLogo(object sender, System.EventArgs e)  
            {  
                //remove image  
                var LogoView = View.ViewWithTag(1234);  
                if (null != LogoView)  
                    LogoView.RemoveFromSuperview();  
            }  
    

    FinishedLaunching in AppDelegate

    public override bool FinishedLaunching(UIApplication app, NSDictionary options)  
            {  
                global::Xamarin.Forms.Forms.Init();  
                LoadApplication(new App());  
                Window = new UIWindow(UIScreen.MainScreen.Bounds);  
                var MainViewController = new MyViewController();  
                Window.RootViewController = MainViewController;  
                Window.MakeKeyAndVisible();  
      
                return true;  
            }  
    

    You could also load ViewController from forms refer to https://stackoverflow.com/questions/63812069/display-viewcontroller-from-native-forms.
    About how to displaythe image in Xamarin.iOS, you could refer to https://learn.microsoft.com/en-us/xamarin/ios/app-fundamentals/images-icons/displaying-an-image?tabs=macos

    ------------update-----------
    Program class(use MyViewController )

     using System.Linq;  
        using System.Text;  
        using UIKit;  
          
        namespace ImageViewDemo.iOS  
        {  
        [Register("AppDelegate")]  
       public class Program : UIApplicationDelegate  
        {  
            private MyViewController MainViewController;  
            public override void FinishedLaunching(UIApplication application)  
            {  
                var Window = new UIWindow(UIScreen.MainScreen.Bounds);  
                MainViewController = new MyViewController();  
                Window.RootViewController = MainViewController;  
                Window.MakeKeyAndVisible();  
            }    
        }  
    }  
    

    Program class(use UIViewController )

    namespace ImageViewDemo.iOS  
    {  
        [Register("AppDelegate")]  
       public class Program : UIApplicationDelegate  
        {  
            private UIViewController MainViewController;  
            private UIImageView LogoImage;  
            public override void FinishedLaunching(UIApplication application)  
            {  
                var Window = new UIWindow(UIScreen.MainScreen.Bounds);  
                MainViewController = new UIViewController();  
                Window.RootViewController = MainViewController;  
                Window.MakeKeyAndVisible();  
                MainViewController.View.BackgroundColor = UIColor.White;  
                LogoImage = new UIImageView(UIImage.FromBundle("Launchscreenlogo"));  
                LogoImage.ContentMode = UIViewContentMode.ScaleAspectFill;  
                LogoImage.Tag = 1234;  
                LogoImage.Frame = MainViewController.View.Frame;  
                MainViewController.View.AddSubview(LogoImage);  
                MainViewController.View.BringSubviewToFront(LogoImage);  
                RunGame();  
            }  
            private void G_RemoveLogo(object sender, System.EventArgs e)  
            {  
                //remove the image  
                ......  
            }  
      
            internal static void RunGame()  
            {  
               ......  
                 
            }  
        }  
    }  
    

    Pay attition to comment out this line of code in Appdelegate.cs

    // [Register("AppDelegate")]  
    

    Best Regards,
    Wenyan Zhang


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


1 additional answer

Sort by: Most helpful
  1. Kim Strasser 1,321 Reputation points
    2021-12-07T22:55:05.933+00:00

    I get an error message if I add the line LoadApplication(new App()); to my Program.cs.
    In addition and I get the following exception if I don't use "var" before Window:

    Foundation.ModelNotImplementedException
    Exception of type 'Foundation.ModelNotImplementedException' was thrown.

    But if I use "var" before Window then it seems to work. I get no error messages with this code:

            private UIViewController MainViewController;
    
            public override void FinishedLaunching(UIApplication app)
            {
                global::Xamarin.Forms.Forms.Init();
                var Window = new UIWindow(UIScreen.MainScreen.Bounds);
                MainViewController = new UIViewController();
                Window.RootViewController = MainViewController;
                Window.MakeKeyAndVisible();
                MainViewController.View.BackgroundColor = UIColor.White;
                LogoImage = new UIImageView(UIImage.FromBundle("Image"));// load the Assets.xcassets image
                LogoImage.ContentMode = UIViewContentMode.ScaleAspectFill;
                LogoImage.Tag = 1234;
                LogoImage.Frame = MainViewController.View.Frame;
                MainViewController.View.AddSubview(LogoImage);
                MainViewController.View.BringSubviewToFront(LogoImage);
    
                RunGame();
            }
    
            private void G_RemoveLogo(object sender, System.EventArgs e)
            {
                //remove the image
                var LogoView = MainViewController.View.ViewWithTag(1234);
                if (null != LogoView)
                    LogoView.RemoveFromSuperview();
            }
    
            internal static void RunGame()
            {
                int w1 = (int)(UIScreen.MainScreen.Bounds.Size.Width * UIScreen.MainScreen.Scale);
                int h1 = (int)(UIScreen.MainScreen.Bounds.Size.Height * UIScreen.MainScreen.Scale);
                game = new Game1(w1, h1);
                game.Run();
            }
    

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.