Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extra bottom and top space in iPhone X in Xamarin form

I'm using XAML for UI design my app is working fine in less then Iphone X device.Only problem in Iphone X it's getting top and bottom Extra space.

If I use below code for Iphone X Safe area enable, it's getting more space in bottom and top. On<Xamarin.Forms.PlatformConfiguration.iOS>().SetUseSafeArea(true);

I got SafeArea layout setting code here SetUserSafeArea

Also i'm using SetHasNavigationBar for disable header navigation title.But there is no luck in Iphone X.

NavigationPage.SetHasNavigationBar(this, false);

Here is actual Output in Iphone X enter image description here

What i 'm missing in code or setting for Iphone X in Xamarin Form

like image 932
Pratius Dubey Avatar asked Feb 04 '23 01:02

Pratius Dubey


1 Answers

I have solved this issue.

Here is an answer.

  1. PCL create an interface to consume in IOS Native App.

     public interface IDeviceInfo
     {
         bool IsIphoneXDevice();
     }
    
  2. Implement this Interface in IOS Native App.

     [assembly: Dependency(typeof(DeviceInfoService))]
     namespace POC.iOS.DependencyServices
     {
         public class DeviceInfoService:IDeviceInfo
         {
             public DeviceInfoService() { }
    
             public bool IsIphoneXDevice()
             {
                 if (UIDevice.CurrentDevice.UserInterfaceIdiom == UIUserInterfaceIdiom.Phone)
                 {
                     if ((UIScreen.MainScreen.Bounds.Height * UIScreen.MainScreen.Scale) == 2436)
                     {
                         return true;
                     }
                 }
                 return false;
             }
         }
     }
    
  3. Call this method in Xamarin form using dependency Service. And write the logic for the iPhone X layout.

     public partial class Page : ContentPage
     {
         public Page()
         {
             InitializeComponent();
    
             var isDeviceIphone = DependencyService.Get<IDeviceInfo>().IsIphoneXDevice();
             if (isDeviceIphone)
             {
                 var safeInsets = On<Xamarin.Forms.PlatformConfiguration.iOS>().SafeAreaInsets();
                 safeInsets.Bottom =20;
                 safeInsets.Top = 20;
                 this.Padding = safeInsets;
             }
         }
     }
    
like image 67
Pratius Dubey Avatar answered Feb 19 '23 06:02

Pratius Dubey