mlauth
mlauth

Reputation: 171

Xamarin.Forms BackgroundImage

i want to implement a background image for the entire app.

I use the global style.xml <item name="android:windowBackground">@drawable/appbg</item> on Android. How can I achive this on iOS?

Is somethig like this possible?

public override bool FinishedLaunching(UIApplication app, 
  NSDictionary options)
{
  ...
  UIWindow.Appearance.BackgroundColor = UIColor.FromPatternImage(
    UIImage.FromFile("appbg.jpg"));
  ...
}

Upvotes: 1

Views: 518

Answers (2)

mlauth
mlauth

Reputation: 171

I solved this with a CustomNavigationPageRenderer.

[assembly: ExportRenderer(typeof(OIdx.XForms.Pages.CustomNavigationPage), 

typeof(OIdx.XForms.iOS.Renderers.CustomNavigationPageRenderer))]
namespace OIdx.XForms.iOS.Renderers
{
    class CustomNavigationPageRenderer : NavigationRenderer
    {
        public override void ViewDidLoad()
        {
            base.ViewDidLoad();
            var img = UIImage.FromBundle("appbg5.jpg");
            UIImageView iv = new UIImageView(img);
            iv.Layer.ZPosition = -1;
            this.View.AddSubview(iv);
        }
    }
}

Upvotes: 1

SushiHangover
SushiHangover

Reputation: 74209

1st: In your AppDelegate assign the Window.BackgroundColor a UIIamge via UIColor.FromPatternImage:

public override bool FinishedLaunching(UIApplication application, NSDictionary launchOptions)
{
    Window.BackgroundColor = UIColor.FromPatternImage(new UIImage("background.png"));
    return true;
}

2nd: In each View that you wish to see this background, you have to set the UIColor to Clear:

View.BackgroundColor = UIColor.Clear;

Note: Multiple image sizes should be used so each device get a resolution appropriate image.

FYI: This is a memory hog that effects the entire life span of the application, it is really bad if this is not a texture/tiled image (iOS or Android) and actually a single image actually spans the entire screen.

Upvotes: 0

Related Questions