Hello @Wenyan Zhang (Shanghai Wicresoft Co,.Ltd.) ,
Thanks for reply. I tried your suggestion but UI is a bit confused like this:
Default done and share button don't closed and added our custom button to ui. I think I didn't implemented it correctly. Could you help me for this?
The implementation I made to turn off the share button:
using App.Helpers;
using App.iOS.CustomRenderer;
using App.Models;
using App.Views;
using Foundation;
using QuickLook;
using System;
using System.IO;
using System.Linq;
using UIKit;
using Xamarin.Forms;
[assembly: Dependency(typeof(DocumentView))]
namespace App.iOS.CustomRenderer
{
public class DocumentView : DocumentViewBase, IDocumentView
{
void IDocumentView.DocumentView(string file, string title)
{
try
{
if (VideoAudioControl(file))
{
OpenVideoAudioPreview(file, title);
}
else
{
UIApplication.SharedApplication.InvokeOnMainThread(() =>
{
PdfPreviewController previewController = new PdfPreviewController();
if (File.Exists(file))
{
previewController.DataSource = new PDFPreviewControllerDataSource(NSUrl.FromFilename(file), title);
UIApplication.SharedApplication.KeyWindow.RootViewController.PresentViewController(previewController, true, null);
}
MessagingCenter.Send("ClosePreviewPopup", "ClosePreviewPopup");
});
}
}
catch (Exception ex)
{
Helper.Log(ex, "IDocumentView.DocumentView", "DocumentView", "IDocumentView.DocumentView(string file, string title)");
}
}
}
public class PDFItem : QLPreviewItem
{
public PDFItem(string title, NSUrl uri)
{
this.Title = title;
this.Url = uri;
}
public string Title { get; set; }
public NSUrl Url { get; set; }
public override NSUrl ItemUrl { get { return Url; } }
public override string ItemTitle { get { return Title; } }
}
public class PDFPreviewControllerDataSource : QLPreviewControllerDataSource
{
PDFItem[] sources;
public PDFPreviewControllerDataSource(NSUrl url, string filename)
{
sources = new PDFItem[1];
sources[0] = new PDFItem(filename, url);
}
public override IQLPreviewItem GetPreviewItem(QLPreviewController controller, nint index)
{
int idx = int.Parse(index.ToString());
if (idx < sources.Length)
return sources.ElementAt(idx);
return null;
}
public override nint PreviewItemCount(QLPreviewController controller)
{
return (nint)sources.Length;
}
}
}
using CoreGraphics;
using App.AppConfigManager.Models;
using Foundation;
using MaterialComponents;
using QuickLook;
using System;
using System.Linq;
using UIKit;
using Xamarin.Forms;
using static System.Net.Mime.MediaTypeNames;
namespace App.iOS.CustomRenderer
{
public class PdfPreviewController : QLPreviewController
{
public PdfPreviewController()
{
var aaa = this.NavigationItem;
}
public override void ViewWillAppear(bool animated)
{
base.ViewWillAppear(animated);
try
{
BeginInvokeOnMainThread(() =>
{
ActionMenuControl();
});
}
catch (Exception ex)
{
Helpers.Helper.Log(ex, "ViewWillAppear", "App.iOS.CustomRenderer.PdfPreviewController", "ViewWillAppear(bool animated)");
}
}
public override void ViewWillLayoutSubviews()
{
base.ViewWillLayoutSubviews();
try
{
BeginInvokeOnMainThread(() =>
{
ActionMenuControl();
});
}
catch (Exception ex)
{
Helpers.Helper.Log(ex, "ViewWillLayoutSubviews", "App.iOS.CustomRenderer.PdfPreviewController", "ViewWillLayoutSubviews()");
}
}
public override void ViewDidLayoutSubviews()
{
base.ViewDidLayoutSubviews();
try
{
BeginInvokeOnMainThread(() =>
{
ActionMenuControl();
});
}
catch (Exception ex)
{
Helpers.Helper.Log(ex, "ViewDidLayoutSubviews", "App.iOS.CustomRenderer.PdfPreviewController", "ViewDidLayoutSubviews()");
}
}
public override void ViewDidAppear(bool animated)
{
base.ViewDidAppear(animated);
try
{
BeginInvokeOnMainThread(() =>
{
ActionMenuControl();
});
}
catch (Exception ex)
{
Helpers.Helper.Log(ex, "ViewDidAppear", "App.iOS.CustomRenderer.PdfPreviewController", "ViewDidAppear()");
}
}
public override void SetNeedsStatusBarAppearanceUpdate()
{
base.SetNeedsStatusBarAppearanceUpdate();
try
{
BeginInvokeOnMainThread(() =>
{
ActionMenuControl();
});
}
catch (Exception ex)
{
Helpers.Helper.Log(ex, "SetNeedsStatusBarAppearanceUpdate", "App.iOS.CustomRenderer.PdfPreviewController", "SetNeedsStatusBarAppearanceUpdate()");
}
}
public void ActionMenuControl()
{
try
{
if (this.ChildViewControllers != null)
{
if (this.ChildViewControllers.Length != 0)
{
try
{
foreach (UINavigationController navigationController in this.ChildViewControllers.Where(x => x is UINavigationController))
{
try
{
if (navigationController.View.Subviews != null)
{
if (navigationController.View.Subviews.Length != 0)
{
try
{
foreach (UINavigationBar navigationBar in navigationController.View.Subviews.Where(x => x is UINavigationBar))
{
if (navigationBar != null)
{
if (navigationBar.Items != null)
{
if (navigationBar.Items.Length != 0)
{
if (navigationBar.Items[0].RightBarButtonItems != null)
{
if (navigationBar.Items[0].RightBarButtonItems.Length != 0)
{
for (int i = 0; i < navigationBar.Items[0].RightBarButtonItems.Length; i++)
{
try
{
string name = navigationBar.Items[0].RightBarButtonItems[i].Action?.Name;
if (name == "_doneButtonTapped:")
continue;
if (name == "_toolbarButtonPressed:")
{
string image1 = navigationBar.Items[0].RightBarButtonItems[i].Image.ToString();
if (image1.Contains("symbol(system: rectangle.and.pencil.and.ellipsis)"))
continue;
string image2 = navigationBar.Items[0].RightBarButtonItems[i].Image.ToString();
if (image2.Contains("symbol(system: magnifyingglass)"))
continue;
}
}
catch (Exception ex)
{
Helpers.Helper.Log(ex, "_doneButtonTapped control error", "App.iOS.CustomRenderer.PdfPreviewController", "_doneButtonTapped control error");
}
navigationBar.Items[0].RightBarButtonItems[i].Enabled = false;
if (navigationBar.Items[0].RightBarButtonItems.Length > 1)
{
foreach (UIBarButtonItem item in navigationBar.Items[0].RightBarButtonItems.Where(x => x.AccessibilityIdentifier == "QLOverlayMarkupButtonAccessibilityIdentifier"))
{
item.Enabled = false;
}
}
}
}
}
}
}
}
}
}
catch (Exception ex)
{ Helpers.Helper.Log(ex, "ActionMenuControl", "App.iOS.CustomRenderer.PdfPreviewController", "ActionMenuControl()"); }
try
{
foreach (UIToolbar subview in navigationController.View.Subviews.Where(x => x is UIToolbar))
{
if (subview.Items != null)
{
if (subview.Items.Length != 0)
{
foreach (UIBarButtonItem item in subview.Items.Where(x => x.AccessibilityIdentifier == "QLOverlayDefaultActionButtonAccessibilityIdentifier"))
{
try
{
string name = item.Action?.Name;
if (name == "_doneButtonTapped:")
continue;
if (name == "_toolbarButtonPressed:")
{
string image1 = item.Image?.ToString();
if (image1.Contains("symbol(system: rectangle.and.pencil.and.ellipsis)"))
continue;
string image2 = item.Image?.ToString();
if (image2.Contains("symbol(system: magnifyingglass)"))
continue;
}
}
catch (Exception ex)
{
Helpers.Helper.Log(ex, "_doneButtonTapped control error", "App.iOS.CustomRenderer.PdfPreviewController", "_doneButtonTapped control error");
}
item.Enabled = false;
}
}
}
}
}
catch (Exception ex)
{ Helpers.Helper.Log(ex, "ActionMenuControl", "App.iOS.CustomRenderer.PdfPreviewController", "ActionMenuControl()"); }
}
}
}
catch (Exception ex)
{ Helpers.Helper.Log(ex, "ActionMenuControl", "App.iOS.CustomRenderer.PdfPreviewController", "ActionMenuControl()"); }
try
{
if (navigationController.Toolbar != null)
{
if (navigationController.Toolbar.Items != null)
{
if (navigationController.Toolbar.Items.Length != 0)
{
foreach (UIBarButtonItem item in navigationController.Toolbar.Items.Where(x => x.AccessibilityIdentifier == "QLOverlayDefaultActionButtonAccessibilityIdentifier"))
{
try
{
string name = item.Action?.Name;
if (name == "_doneButtonTapped:")
continue;
if (name == "_toolbarButtonPressed:")
{
string image1 = item.Image?.ToString();
if (image1.Contains("symbol(system: rectangle.and.pencil.and.ellipsis)"))
continue;
string image2 = item.Image?.ToString();
if (image2.Contains("symbol(system: magnifyingglass)"))
continue;
}
}
catch (Exception ex)
{
Helpers.Helper.Log(ex, "_doneButtonTapped control error", "App.iOS.CustomRenderer.PdfPreviewController", "_doneButtonTapped control error");
}
item.Enabled = false;
}
}
}
}
}
catch (Exception ex)
{ Helpers.Helper.Log(ex, "ActionMenuControl", "App.iOS.CustomRenderer.PdfPreviewController", "ActionMenuControl()"); }
}
}
catch (Exception ex)
{ Helpers.Helper.Log(ex, "ActionMenuControl", "App.iOS.CustomRenderer.PdfPreviewController", "ActionMenuControl()"); }
}
}
}
catch (Exception ex)
{
Helpers.Helper.Log(ex, "ActionMenuControl", "App.iOS.CustomRenderer.PdfPreviewController", "ActionMenuControl()");
}
}
}
}
After your suggestion I did this:
using App.Helpers;
using App.iOS.CustomRenderer;
using App.Models;
using App.Views;
using Foundation;
using QuickLook;
using System;
using System.IO;
using System.Linq;
using UIKit;
using Xamarin.Forms;
[assembly: Dependency(typeof(DocumentView))]
namespace App.iOS.CustomRenderer
{
public class DocumentView : DocumentViewBase, IDocumentView
{
void IDocumentView.DocumentView(string file, string title)
{
try
{
if (VideoAudioControl(file))
{
OpenVideoAudioPreview(file, title);
}
else
{
UIApplication.SharedApplication.InvokeOnMainThread(() =>
{
PdfPreviewController previewController = new PdfPreviewController(file, title);
if (File.Exists(file))
{
previewController.DataSource = new PDFPreviewControllerDataSource(NSUrl.FromFilename(file), title);
UIApplication.SharedApplication.KeyWindow.RootViewController.PresentViewController(previewController, true, null);
}
MessagingCenter.Send("ClosePreviewPopup", "ClosePreviewPopup");
});
}
}
catch (Exception ex)
{
Helper.Log(ex, "IDocumentView.DocumentView", "DocumentView", "IDocumentView.DocumentView(string file, string title)");
}
}
}
public class PDFItem : QLPreviewItem
{
public PDFItem(string title, NSUrl uri)
{
this.Title = title;
this.Url = uri;
}
public string Title { get; set; }
public NSUrl Url { get; set; }
public override NSUrl ItemUrl { get { return Url; } }
public override string ItemTitle { get { return Title; } }
}
public class PDFPreviewControllerDataSource : QLPreviewControllerDataSource
{
PDFItem[] sources;
public PDFPreviewControllerDataSource(NSUrl url, string filename)
{
sources = new PDFItem[1];
sources[0] = new PDFItem(filename, url);
}
public override IQLPreviewItem GetPreviewItem(QLPreviewController controller, nint index)
{
int idx = int.Parse(index.ToString());
if (idx < sources.Length)
return sources.ElementAt(idx);
return null;
}
public override nint PreviewItemCount(QLPreviewController controller)
{
return (nint)sources.Length;
}
}
}
using CoreGraphics;
using App.AppConfigManager.Models;
using Foundation;
using MaterialComponents;
using QuickLook;
using System;
using System.Linq;
using UIKit;
using Xamarin.Forms;
using static System.Net.Mime.MediaTypeNames;
namespace App.iOS.CustomRenderer
{
public class PdfPreviewController : QLPreviewController
{
string file = "";
string title = "";
public PdfPreviewController(string file, string title)
{
this.file = file;
this.title = title;
var aaa = this.NavigationItem;
}
public override void ViewDidLoad()
{
base.ViewDidLoad();
UIButton clickbutton = UIButton.FromType(UIButtonType.System);
clickbutton.Frame = new CoreGraphics.CGRect(0, 90, 40, 40);
clickbutton.SetTitle("click", UIControlState.Normal);
View.AddSubview(clickbutton);// Add a click button for testing
UIViewController customVC = new UIViewController();// add a custonVC to present the QLPreviewController
// done button
UIButton doneButton = UIButton.FromType(UIButtonType.System);
doneButton.Frame = new CGRect(UIScreen.MainScreen.Bounds.Width - 40, 20, 40, 40);
doneButton.SetTitle("Done", UIControlState.Normal);
doneButton.TouchUpInside += delegate
{
customVC.DismissModalViewController(true);// dismiss the customVC
};
customVC.View.AddSubview(doneButton);//add done button on the viewcontroller
// QLPreviewController and the DataSource, you can replace it by yours
var vc = new QLPreviewController();
vc.DataSource = new PDFPreviewControllerDataSource(NSUrl.FromFilename(file), title);
customVC.AddChildViewController(vc);
vc.View.Frame = new CoreGraphics.CGRect(0, 0, UIScreen.MainScreen.Bounds.Width, UIScreen.MainScreen.Bounds.Height - 20 - 40);// 20 is y of done button , 40 is height of done button
customVC.View.AddSubview(vc.View);// add QLPreviewController.view to this customVC
customVC.ModalPresentationStyle = UIModalPresentationStyle.FullScreen;
// click the button to present
clickbutton.TouchUpInside += delegate
{
// present the customVC
this.PresentViewController(customVC, true, null);
};
}
}
}
But UI mixed like above image. Could you assist me?