【发布时间】:2021-02-04 17:00:42
【问题描述】:
iOS 应用程序在 iOS 版本 13+ 设备的屏幕底部显示空白,但在 iOS 12 设备上没问题。
这是一个 Xamarin.Forms 项目,我使用了 CustomTabbedPage,它使用 Xamarin.iOS 项目中定义的 TabbedPageRenderer 类呈现。
请建议我好的方法或解决方案。
截图:
代码sn-p:
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();
}
}
}
}
【问题讨论】:
-
可能是安全区。尝试
ios:Page.UseSafeArea=False,如this ms docs -
尝试resize the page's frame,如线程中所述。
-
非常感谢 Jack Hua,现在它按预期工作了。 :)
-
@YashwantSinghRathore 我在那里添加了一个答案,你能接受吗(点击这个答案左上角的☑️),以便我们可以帮助更多有同样问题的人:)。
标签: xamarin xamarin.forms c#-4.0 xamarin.ios xcode4