从QLPreviewController删除不需要的顶部空间

时间:2019-06-11 16:49:42

标签: c# xamarin.ios qlpreviewcontroller

在我的应用程序中,我使用了QLPreviewController来显示一些pdf文件。一切正常,但是QLPreviewController的内容顶部有一个多余的空间,找不到删除它的方法。

这就是我的写法。(请注意,我是Xamarin.iOS和QLPreviewController的新手)

public partial class ReportsViewController : UIViewController
{
    public override void ViewDidLoad()
    {
        base.ViewDidLoad();

        //Customize navigation bar for QLPreviewController
        if(UIDevice.CurrentDevice.CheckSystemVersion(11, 0)) // There is a bug with barTintColor on QLPreviewController for iOS 11 if you are showing it via presentViewController: animated:
        {
            var color = UIColor.FromRGB(red: 23, green: 61, blue: 86).CGColor;
            var rect = new CGRect(x: 0.0, y: 0.0, width: 1.0, height: 1.0);
            var alpha = color.Alpha;
            var opaque = alpha == 1;
            UIGraphics.BeginImageContextWithOptions(rect.Size, opaque, 0);
            var context = UIGraphics.GetCurrentContext();
            context.SetFillColor(color);
            context.FillRect(rect);
            var image = UIGraphics.GetImageFromCurrentImageContext();
            UIGraphics.EndImageContext();

            UINavigationBar.AppearanceWhenContainedIn(typeof(QLPreviewController)).SetBackgroundImage(image, UIBarMetrics.Default);

        }
        else // If iOS version is below 11.0
        {
            UINavigationBar.AppearanceWhenContainedIn(typeof(QLPreviewController)).BarTintColor = UIColor.FromRGBA(red: 255, green: 0, blue: 0, alpha: 1.0f);
        }

        UINavigationBar.AppearanceWhenContainedIn(typeof(QLPreviewController)).BarStyle = UIBarStyle.Black;
        UINavigationBar.AppearanceWhenContainedIn(typeof(QLPreviewController)).TintColor = UIColor.White;

        var reportSavedPath = "path to pdf file";
        var reportName = "Report name";
        var previewController = new QLPreviewController();
        var url = new NSUrl(reportSavedPath, true);
        var _dataSource = new PreviewControllerSource(this, url, reportName);
        previewController.DataSource = _dataSource;
        PresentViewController(previewController, true, completionHandler: null);
    }
}

public partial class ReportsViewController : UIViewController
{
    class PreviewControllerSource : QLPreviewControllerDataSource
    {
        ReportsViewController _parentClass = null;
        NSUrl _url = null;
        string _title = null;

        public PreviewControllerSource(ReportsViewController parentClass, NSUrl url, string title)
        {
            _parentClass = parentClass;
            _url = url;
            _title = title;
        }

        public override nint PreviewItemCount(QLPreviewController controller)
        {
            return 1;
        }

        public override IQLPreviewItem GetPreviewItem(QLPreviewController controller, nint index)
        {
            return new PreviewItem { title = _title, url = _url };
        }
    }
    public class PreviewItem : QLPreviewItem
    {
        public string title { get; set; }
        public NSUrl url { get; set; }
        public override string ItemTitle { get { return title; } }
        public override NSUrl ItemUrl { get { return url; } }
    }
}

这是加载pdf时的样子

enter image description here

有人可以帮助我删除不需要的空间吗?

1 个答案:

答案 0 :(得分:0)

不知道为什么必须在代码中Customize navigation bar for QLPreviewController。我将其删除,效果很好。

我将为您提供两种摆脱不必要的树皮的方法:

1。删除代码:

//UINavigationBar.AppearanceWhenContainedIn(typeof(QLPreviewController)).SetBackgroundImage(image, UIBarMetrics.Default);

2。如果您确实需要代码来定制导航栏,请在PresentViewController之前添加以下代码行:

//Add this line before PresentViewController
UINavigationBar.AppearanceWhenContainedIn(typeof(QLPreviewController)).SetBackgroundImage(null, UIBarMetrics.Default);

PresentViewController(previewController, true, completionHandler: null);

更新

花了整个下午的时间,只找到一个不太完美的解决方案。首先,我想说的是,您的{{1}的navigationBarviewController的{​​{1}}是两个不同的小节。

要设置导航栏颜色,只需在navigationBar中进行设置,这将影响项目中整个QLPreviewController的颜色。

AppDelegate

要更改navigationBar中的 public override bool FinishedLaunching(UIApplication application, NSDictionary launchOptions) { // Override point for customization after application launch. // If not required for your application you can safely delete this method UINavigationBar.Appearance.BarTintColor = UIColor.FromRGBA(red: 255, green: 0, blue: 0, alpha: 1.0f); UINavigationBar.Appearance.BarStyle = UIBarStyle.BlackOpaque; UINavigationBar.Appearance.TintColor = UIColor.White; return true; } ,可以使用navigationbarColor函数中的代码:

QLPreviewControl

唯一不太完美的是test函数将一直调用到演示完成为止,因此您将在一段时间内看到navigationBar的默认颜色,然后它将变为所需的颜色。 / p>

如果您使用推式操作而不是模式操作,则可以在public partial class ViewController : UIViewController { QLPreviewController previewController; public ViewController(IntPtr handle) : base(handle) { } public override void ViewDidLoad() { base.ViewDidLoad(); this.AutomaticallyAdjustsScrollViewInsets = false; this.ExtendedLayoutIncludesOpaqueBars = false; this.EdgesForExtendedLayout = UIRectEdge.None; string path = NSBundle.MainBundle.PathForResource("myPdf.pdf",""); var reportName = "Report name"; previewController = new QLPreviewController(); var url = new NSUrl(path, true); var _dataSource = new PreviewControllerSource(this, url, reportName); previewController.DataSource = _dataSource; this.NavigationController.PresentViewController(previewController, true, test); } public void test() { var firstChild = previewController.ChildViewControllers[0]; if (firstChild is UINavigationController) { var naviVc = firstChild as UINavigationController; naviVc.NavigationBar.BarTintColor = UIColor.Red ; } } } 内调用test函数:

test

我在这里上传了样本,您可以检查它:navigationBar-color-xamarin.forms