应用程序在 iOS 13+ iPhone 的屏幕底部显示白色 space 但在 iOS 12 iPhone 上没有问题
App showing white space at bottom of screen for iOS 13+ iPhones but it is fine with iOS 12 iPhones
iOS 应用程序在 iOS 版本 13+ 设备的屏幕底部显示白色 space,但在 iOS 12 台设备上没问题。
这是一个 Xamarin.Forms 项目,我使用了 CustomTabbedPage,它使用 Xamarin.iOS 项目中定义的 TabbedPageRenderer class 呈现。
请建议我好的方法或解决方案。
截图:
代码片段:
using MyProject.Infrastructure.Controls;
using CoreGraphics;
using System;
using System.ComponentModel;
using System.Threading.Tasks;
using UIKit;
using Xamarin.Forms;
using Xamarin.Forms.Platform.iOS;
[assembly: ExportRenderer(typeof(CustomTabbedPage), typeof(MyProject.iOS.Renderers.TabbedPageRenderer))]
namespace MyProject.iOS.Renderers
{
public class TabbedPageRenderer : TabbedRenderer
{
private bool _disposed;
private const int TabBarHeight = 49;
private int systemVersion = 0;
protected override void OnElementChanged(VisualElementChangedEventArgs e)
{
base.OnElementChanged(e);
if (e.OldElement != null)
{
e.OldElement.PropertyChanged -= Element_PropertyChanged;
}
if (e.NewElement != null)
{
e.NewElement.PropertyChanged += Element_PropertyChanged;
}
}
public override void ViewWillLayoutSubviews()
{
if (Element is CustomTabbedPage element)
{
OnTabBarHidden(!element.Visibility);
}
}
private void Element_PropertyChanged(object sender, PropertyChangedEventArgs e)
{
if (e.PropertyName == CustomTabbedPage.VisibilityProperty.PropertyName)
{
if (Element is CustomTabbedPage element)
{
OnTabBarHidden(!element.Visibility);
}
}
}
protected override void Dispose(bool disposing)
{
base.Dispose(disposing);
_disposed = true;
}
private async void OnTabBarHidden(bool isHidden)
{
if (_disposed || Element == null || TabBar == null)
{
return;
}
await SetTabBarVisibility(isHidden);
}
private async Task SetTabBarVisibility(bool hide)
{
TabBar.Opaque = false;
if (hide)
{
TabBar.Alpha = 0;
}
UpdateFrame(hide);
// Show / Hide TabBar
TabBar.Hidden = hide;
RestoreFonts();
// Animate appearing
if (!hide)
{
await UIView.AnimateAsync(1.0f, () => TabBar.Alpha = 1);
}
TabBar.Opaque = true;
ResizeViewControllers();
RestoreFonts();
}
private void UpdateFrame(bool isHidden)
{
CoreGraphics.CGRect tabFrame = TabBar.Frame;
tabFrame.Height = isHidden ? 0 : TabBarHeight;
TabBar.Frame = tabFrame;
}
private void RestoreFonts()
{
// Workaround to restore custom fonts:
if (TabBar.Items != null)
{
foreach (UITabBarItem item in TabBar.Items)
{
string text = item.Title;
item.Title = "";
item.Title = text;
}
}
}
private void ResizeViewControllers()
{
foreach (UIViewController child in ChildViewControllers)
{
child.View.SetNeedsLayout();
child.View.SetNeedsDisplay();
}
}
}
}
您尝试过嵌入标签栏控制器吗? (编辑器 > 嵌入 > 标签栏控制器)
每次将 TabBar.Hidden
设置为 ture/false 时调整页面框架的大小:
public override void ViewDidLayoutSubviews()
{
base.ViewDidLayoutSubviews();
var frame = View.Frame;
var tabBarFrame = TabBar.Frame;
if ((Element as TabbedPageBottom).IsHide)
{
TabBar.Hidden = true;
Page.ContainerArea = new Rectangle(0, 0, frame.Width, frame.Height);
}
else
{
TabBar.Hidden = false;
Page.ContainerArea = new Rectangle(0, 0, frame.Width, frame.Height-tabBarFrame.Height);
}
}
iOS 应用程序在 iOS 版本 13+ 设备的屏幕底部显示白色 space,但在 iOS 12 台设备上没问题。
这是一个 Xamarin.Forms 项目,我使用了 CustomTabbedPage,它使用 Xamarin.iOS 项目中定义的 TabbedPageRenderer class 呈现。
请建议我好的方法或解决方案。
截图:
代码片段:
using MyProject.Infrastructure.Controls;
using CoreGraphics;
using System;
using System.ComponentModel;
using System.Threading.Tasks;
using UIKit;
using Xamarin.Forms;
using Xamarin.Forms.Platform.iOS;
[assembly: ExportRenderer(typeof(CustomTabbedPage), typeof(MyProject.iOS.Renderers.TabbedPageRenderer))]
namespace MyProject.iOS.Renderers
{
public class TabbedPageRenderer : TabbedRenderer
{
private bool _disposed;
private const int TabBarHeight = 49;
private int systemVersion = 0;
protected override void OnElementChanged(VisualElementChangedEventArgs e)
{
base.OnElementChanged(e);
if (e.OldElement != null)
{
e.OldElement.PropertyChanged -= Element_PropertyChanged;
}
if (e.NewElement != null)
{
e.NewElement.PropertyChanged += Element_PropertyChanged;
}
}
public override void ViewWillLayoutSubviews()
{
if (Element is CustomTabbedPage element)
{
OnTabBarHidden(!element.Visibility);
}
}
private void Element_PropertyChanged(object sender, PropertyChangedEventArgs e)
{
if (e.PropertyName == CustomTabbedPage.VisibilityProperty.PropertyName)
{
if (Element is CustomTabbedPage element)
{
OnTabBarHidden(!element.Visibility);
}
}
}
protected override void Dispose(bool disposing)
{
base.Dispose(disposing);
_disposed = true;
}
private async void OnTabBarHidden(bool isHidden)
{
if (_disposed || Element == null || TabBar == null)
{
return;
}
await SetTabBarVisibility(isHidden);
}
private async Task SetTabBarVisibility(bool hide)
{
TabBar.Opaque = false;
if (hide)
{
TabBar.Alpha = 0;
}
UpdateFrame(hide);
// Show / Hide TabBar
TabBar.Hidden = hide;
RestoreFonts();
// Animate appearing
if (!hide)
{
await UIView.AnimateAsync(1.0f, () => TabBar.Alpha = 1);
}
TabBar.Opaque = true;
ResizeViewControllers();
RestoreFonts();
}
private void UpdateFrame(bool isHidden)
{
CoreGraphics.CGRect tabFrame = TabBar.Frame;
tabFrame.Height = isHidden ? 0 : TabBarHeight;
TabBar.Frame = tabFrame;
}
private void RestoreFonts()
{
// Workaround to restore custom fonts:
if (TabBar.Items != null)
{
foreach (UITabBarItem item in TabBar.Items)
{
string text = item.Title;
item.Title = "";
item.Title = text;
}
}
}
private void ResizeViewControllers()
{
foreach (UIViewController child in ChildViewControllers)
{
child.View.SetNeedsLayout();
child.View.SetNeedsDisplay();
}
}
}
}
您尝试过嵌入标签栏控制器吗? (编辑器 > 嵌入 > 标签栏控制器)
每次将 TabBar.Hidden
设置为 ture/false 时调整页面框架的大小:
public override void ViewDidLayoutSubviews()
{
base.ViewDidLayoutSubviews();
var frame = View.Frame;
var tabBarFrame = TabBar.Frame;
if ((Element as TabbedPageBottom).IsHide)
{
TabBar.Hidden = true;
Page.ContainerArea = new Rectangle(0, 0, frame.Width, frame.Height);
}
else
{
TabBar.Hidden = false;
Page.ContainerArea = new Rectangle(0, 0, frame.Width, frame.Height-tabBarFrame.Height);
}
}