Extra bottom and top space in iPhone X in Xamarin form

独自空忆成欢 提交于 2020-08-06 09:15:10

问题


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

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


回答1:


I have solved this issue.

Here is a Answer.

  1. PCL create a 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 logic for 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;
            }
        }
    }
    



回答2:


The way I got the proper screen size to work on iOS, was by simply adding the proper splash screen images.

For example, in my iOS project I added into my Resources folder, an image named Default-812h@3x.png, and the dimensions of the image were 1125x2436.

And then in my Info.plist file, I added the following code under the UILaunchImages key:

<key>UILaunchImages</key>
    <array>
        <dict>
            <key>UILaunchImageMinimumOSVersion</key>
            <string>8.0</string>
            <key>UILaunchImageName</key>
            <string>Default-812h</string>
            <key>UILaunchImageOrientation</key>
            <string>Portrait</string>
            <key>UILaunchImageSize</key>
            <string>{375, 812}</string>
        </dict>
        ... other launch images ...
    </array>



回答3:


I recently had the same issue. What I found out was that iOS determines if your app can handle iPhone X by the splash screen. There were no splash screen images that would work. I had to create a storyboard and use that for my splash screen. This link should help you out: https://developer.xamarin.com/guides/ios/application_fundamentals/working_with_images/launch-screens/




回答4:


SafeAreaInsets did trick for us.

In AppDelegate.CS,

base.FinishedLaunching(uiApplication, launchOptions);

should be replaced with below code:

var result = base.FinishedLaunching(uiApplication, launchOptions);
            try {
                if (UIApplication.SharedApplication.KeyWindow != null) {
                    double top = 0;
                    double bottom = 0;
                    if (UIDevice.CurrentDevice.CheckSystemVersion(11, 0)) {
                        top = UIApplication.SharedApplication.KeyWindow.SafeAreaInsets.Top;
                        bottom = UIApplication.SharedApplication.KeyWindow.SafeAreaInsets.Bottom;
                    }
//Store safe area values using NSUserDefaults.StandardUserDefaults 
                    DependencyService.Get<ISettingsService>().AddOrUpdateValue("IPhoneXSafeTop", top);
                    DependencyService.Get<ISettingsService>().AddOrUpdateValue("IPhoneXSafeBottom", bottom);
                    DependencyService.Get<ISettingsService>().Save();
                }
            } catch (Exception ex) {
                Console.WriteLine("Ex in FinishedLaunching: " + ex.Message);
            }
            return result;

In PCL, xaml.cs file add below code in Constructor:

var IPhoneXSafeBottom = DependencyService.Get<ISettingsService> ().GetValueOrDefault<Double> ("IPhoneXSafeBottom", 0);
var IPhoneXSafeTop = DependencyService.Get<ISettingsService> ().GetValueOrDefault<Double> ("IPhoneXSafeTop", 0);
            if (IPhoneXSafeBottom > 0) {
                MainLayout.Padding = new Thickness(0, IPhoneXSafeTop, 0, IPhoneXSafeBottom);

            }


来源:https://stackoverflow.com/questions/49166915/extra-bottom-and-top-space-in-iphone-x-in-xamarin-form

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!