Xamarin Forms iOS Project Background Images

KingOfTheNorthKingOfTheNorth ✭✭Member ✭✭

Hi Everyone,

I am working on a Xamarin Forms Project. I have all the appropriate Background Image assets to target iPhone 8 and up (total 5). My questions is how to I get them to appear appropriately?

Best Answer

Answers

  • LucasZhangLucasZhang Xamurai Member, Xamarin Team Xamurai

    If you want to let the background image fit the size both on iPhone 8(plus) and iPhoneX(max..).

    Firstly , create an extra image set in Asset . For example backImg and backImg_ipx .And in backImg_ipx put the new background image in 3x(1125 * 2436).

    Then use DependencyService get the info of the device

    in Forms

    public interface IGetDeviceInfo
    {
       bool IsiphoneX ();
    }
    

    in iOS

    using UIKit;
    using Xamarin.Forms;
    using xxx.iOS;
    
    [assembly: Dependency(typeof(IsiPhoneXImplemention))]
    namespace xxx.iOS
    {
        public class IsiPhoneXImplemention : IGetDeviceInfo
        {
            public bool IsiphoneX()
            {
    
                bool isiPhoneX = false;
    
                if(UIDevice.CurrentDevice.CheckSystemVersion(11, 0))
                {
                    if(UIApplication.SharedApplication.Delegate.GetWindow().SafeAreaInsets.Bottom>0.0)
                    {
                        isiPhoneX = true;
                    }
                }
    
                return isiPhoneX;
            }
        }
    }
    

    And you can set the different image in forms

    if(Device.RuntimePlatform==Device.iOS)
    {
       var isiPhoneX = DependencyService.Get<IGetDeviceInfo>().IsiphoneX();
    
        if(isiPhoneX)
        {
    
        }
        else
        {
    
        }
    }
    
  • KingOfTheNorthKingOfTheNorth ✭✭ Member ✭✭

    @LucasZhang Thank you for your input I have 5 phones to target. iPhone 8 (750x1334), iPhone 8+ (1242X2208), iPhone Xr (828x1792), iPhone Xs (1125x2436), and iPhone Xs MAX (1242x2688). All of which I have the appropriate images for. Maybe I'm a little confused as I am a beginner in XF. I am aware of the Assests.xcassets folder, but I'm not sure were the 5 images go as I only see Vector, 1x, 2x, R4, and 3x slots with the iPhone text label beneath the horizontal line.

    i noticed you are using UIDevice.CurrentDevice.CheckSystemVersion(11, 0) why are you checking for a version 11?

  • KingOfTheNorthKingOfTheNorth ✭✭ Member ✭✭

    @LucasZhang Hi Lucas, the solution you provided works great for non iPhoneX devices. The problem im running into is that im getting a null reference exception for UIApplication.SharedApplication.Delegate.GetWindow().SafeAreaInsets.Bottom>0.0.

    Any reason why?

Sign In or Register to comment.