Set background image in UIViewController (MonoTouch)

16,000

Solution 1

try setting the BackgroundColor of your View

myview.BackgroundColor = UIColor.FromPatternImage(UIImage.FromFile("myimage.png"));

Solution 2

I voted up for the other answers, however today IPhone has different sizes and the correct way to load the image is using UIImage.FromBundle method:

Here is the assert catalog in the project:

enter image description here

To manage the images:

enter image description here

public override void ViewDidLoad()
{
    base.ViewDidLoad();

    // replace "name" with the desired name in the asset catalog
    this.View.BackgroundColor = UIColor.FromPatternImage(UIImage.FromBundle("name"));

}

Solution 3

Try adding something like the following to your MyViewNameViewController.cs:

public override void ViewDidLoad()
{
    base.ViewDidLoad();
    this.View.BackgroundColor = UIColor.FromPatternImage(UIImage.FromFile("splash.png"));
}
Share:
16,000
Brian David Berman
Author by

Brian David Berman

Updated on July 18, 2022

Comments

  • Brian David Berman
    Brian David Berman almost 2 years

    I have a screen called HomeScreen which implements UIViewController. I wish to use a background image for this screen. Is there an event that I can override to set this background image in the HomeScreen.cs file?

  • nhenrique
    nhenrique about 9 years
    is there any way to make the image scale to the view size and still using it as a background color?
  • Elliot Blackburn
    Elliot Blackburn over 8 years
    @nhenrique I am wondering this as well, did you find a solution?
  • Old Fox
    Old Fox over 6 years
    @nhenrique I've added a new answer for your question
  • Old Fox
    Old Fox over 6 years
    @ElliotBlackburn I've added a new answer to solve the different sizes IPhone problem
  • Elliot Blackburn
    Elliot Blackburn over 6 years
    @OldFox wow this is old, thanks for updating it. Hopefully it'll help someone else in the future. I don't even use Xamarin anymore haha