我遇到了这个问题,我需要在我的“依赖服务”中获取导航栏的高度。
目前,我在这里要遵循的内容仍然很棘手。我尝试了在stackoverflow和google中找到的所有内容,但没有人适合我。
这里是我的代码:
[assembly: Dependency(typeof(DeviceInfo))]
namespace Wicket.App.Mobile.iOS.Framework
{
public class DeviceInfo : IDeviceInfo
{
public float StatusBarHeight => (float)UIApplication.SharedApplication.StatusBarFrame.Size.Height;
public float NavigationBarHeight => GetNavigationBarHeight();
public static UINavigationController NavigationController { get; set; }
public float GetNavigationBarHeight()
{
//Get navigation bar height
return 0;
}
}
}
我已经完成了android部分,效果很好。现在唯一的问题是在iOS中。我尝试在AppDelegate中获取navigationcontroller的实例,以便可以像这样NavigationBar.Bounds.Height;
答案 0 :(得分:0)
我认为这应该可行:
var navheight = GetTopViewController().NavigationController.NavigationBar.Frame.Height;
public static UIViewController GetTopViewController()
{
var window = UIApplication.SharedApplication.KeyWindow;
var vc = window.RootViewController;
while (vc.PresentedViewController != null)
vc = vc.PresentedViewController;
if (vc is UINavigationController navController)
vc = navController.ViewControllers.Last();
return vc;
}
答案 1 :(得分:0)
解决方案:
如何在IDeviceInfo
内部的函数中传递viewController的实例作为参数?
尝试一下:
public void getNaviHeight(ContentPage vc)
{
var renderer = Platform.GetRenderer(vc);
if (renderer == null)
{
renderer = RendererFactory.GetRenderer(vc);
Platform.SetRenderer(vc, renderer);
}
var viewController = renderer.ViewController;
var h = viewController.NavigationController?.NavigationBar.Frame.Height;
}
并使用依赖项:
public MainPage ()
{
DependencyService.Get<IDeviceInfo>().getNaviHeight(this);
}
答案 2 :(得分:0)
这对我有用:
var navigationBar = UIApplication.SharedApplication.KeyWindow.RootViewController.View.Subviews[0].Subviews.OfType<UINavigationBar>().FirstOrDefault();
if(navigationBar != null)
{
// continue here...
}