How to fill safeareas for Xamarin iOS project only with code

Dubi Sharon 1 Reputation point
2021-04-06T12:49:17.897+00:00

Hello,
I have an iOS Project (code only, no storyboard or forms)
I would like to have a custom background image to fill the whole screen including the safe areas in my view controller.
How can this be done using only code within the VC ?
Thank you
Dubi

Xamarin
Xamarin
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,378 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Leon Lu (Shanghai Wicresoft Co,.Ltd.) 79,781 Reputation points Microsoft Vendor
    2021-04-07T08:58:18.533+00:00

    Hello,​

    Welcome to our Microsoft Q&A platform!

    Yes, you can add following code in your VC.

       [Register("UIViewController1")]  
           public class UIViewController1 : UIViewController  
           {  
               public UIViewController1()  
               {  
               }  
         
               public override void DidReceiveMemoryWarning()  
               {  
                   // Releases the view if it doesn't have a superview.  
                   base.DidReceiveMemoryWarning();  
         
                   // Release any cached data, images, etc that aren't in use.  
               }  
         
               public override void ViewDidLoad()  
               {  
                  // View = new UniversalView();  
         
                   base.ViewDidLoad();  
         
                   // Perform any additional setup after loading the view  
                   
                   UIImageView textView = new UIImageView();  
                   textView.Image = UIImage.FromBundle("test");  
                   textView.Frame = UIScreen.MainScreen.Bounds;  
                   textView.InsetsLayoutMarginsFromSafeArea = false;  
                   this.View.Add(textView);  
               }  
           }  
    

    I add my test.png image to the Asset

    85246-image.png

    Here is my running screenshot.

    85227-image.png

    Best Regards,

    Leon Lu


    If the response is helpful, please click "Accept Answer" and upvote it.

    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.


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.