iPhone X中的Xamarin形式的额外底部和顶部空间

时间:2018-03-08 06:39:58

标签: c# xamarin xamarin.forms xamarin.ios

我使用XAML进行UI设计我的应用程序在Iphone X设备中工作得很好。在Iphone X中只有顶部和底部的额外空间问题。

如果我使用以下代码启用Iphone X Safe区域,它会在底部和顶部获得更多空间。 On<Xamarin.Forms.PlatformConfiguration.iOS>().SetUseSafeArea(true);

我在这里获得了SafeArea布局设置代码SetUserSafeArea

我也使用SetHasNavigationBar来禁用标题导航标题。但是在Iphone X中没有运气。

NavigationPage.SetHasNavigationBar(this, false);

这是Iphone X中的实际输出 enter image description here

我在Xamarin表单中的Iphone X代码或设置中缺少的内容

4 个答案:

答案 0 :(得分:4)

我已经解决了这个问题。

这是答案。

  1. PCL创建一个在IOS Native App中使用的界面。

    public interface IDeviceInfo
    {
        bool IsIphoneXDevice();
    }
    
  2. 在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. 使用依赖关系Service.And为IPhone X布局编写逻辑,以Xamarin形式调用此方法。

    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;
            }
        }
    }
    

答案 1 :(得分:2)

我最近遇到了同样的问题。我发现iOS确定您的应用程序是否可以通过启动画面处理iPhone X.没有可用的闪屏图像。我不得不创建一个故事板并将其用于我的启动画面。此链接可以帮助您:https://developer.xamarin.com/guides/ios/application_fundamentals/working_with_images/launch-screens/

答案 2 :(得分:1)

我在iOS上使用合适的屏幕尺寸的方法是简单地添加正确的闪屏图像。

例如,在我的iOS项目中,我在Resources文件夹中添加了一个名为Default-812h@3x.png的图片,图片的尺寸为 1125x2436

然后在我的Info.plist文件中,我在UILaunchImages键下添加了以下代码:

<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 :(得分:1)

SafeAreaInsets确实对我们有用。

在AppDelegate.CS中,

base.FinishedLaunching(uiApplication, launchOptions);

应替换为以下代码:

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;

在PCL中,xaml.cs文件在构造函数中添加以下代码:

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);

            }
相关问题