【问题标题】:Extra bottom and top space in iPhone X in Xamarin formXamarin 形式的 iPhone X 中的额外底部和顶部空间
【发布时间】:2018-08-16 10:34:34
【问题描述】:

我正在使用 XAML 进行 UI 设计,我的应用在少于 Iphone X 的设备上运行良好。唯一的问题是 Iphone X 的顶部和底部有额外的空间。

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

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

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

NavigationPage.SetHasNavigationBar(this, false);

这是 Iphone X 中的实际输出

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

【问题讨论】:

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


    【解决方案1】:

    我已经解决了这个问题。

    这是一个答案。

    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. 使用依赖服务以 Xamarin 形式调用此方法。并编写 iPhone X 布局的逻辑。

       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】:

      我获得在 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】:

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

        【讨论】:

          【解决方案4】:

          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 文件在 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);
          
                      }
          

          【讨论】:

          • 试过这段代码没有任何变化,我们不需要添加大代码来保存常量值。
          • 要在用户杀死并重新打开应用程序时保持值,我们需要此代码.. 启动画面和背景图像需要大小为 1125x2436。
          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2016-12-18
          • 2017-09-25
          • 1970-01-01
          • 2020-06-04
          • 2020-04-18
          • 1970-01-01
          • 2017-08-21
          相关资源
          最近更新 更多