UIDocumentInteractionController更改已呈现视图的标题

20

我正在使用UIDocumentInteractionController来显示PDF文件。我的文件使用编码的文件名存储在文件系统中,并不是用户友好的名称。我可以访问一个更友好的文件名,但该文件不是以此名称存储的(出于唯一性原因)。

当我将文档加载到UIDocumentInteractionController中时,视图会在标题栏中显示不友好的“真实”文件名。

是否有任何方法可以更改由UIDocumentInteractionController呈现的显示标题?

2个回答

44

UIDocumentInteractionController有一个name属性,你可以设置为用户友好的名称。该名称将用于呈现文档时的标题栏。


非常感谢,这很有帮助。 - swiftBoy
@RDC,你能给我展示一下样例代码吗?因为我的代码无法运行。 - Seema Sharma
这个解决方案不起作用。它仍然显示文件名。 - Castor

0

这是Xamarin(C#)代码,但我希望它能帮到你

public class PreviewFile : IPreviewFile
{
    public PreviewFile()
    {
    }

    public void Preview(string file, string title)
    {
        var window = UIApplication.SharedApplication.KeyWindow;
        var vc = window.RootViewController;
        bool isUrl = file.StartsWith("file:", StringComparison.OrdinalIgnoreCase);
        var url = isUrl ? new NSUrl(file) : NSUrl.FromFilename(file);
        
        var controller = UIDocumentInteractionController.FromUrl(url);

        controller.Delegate = new MyInteractionDelegate(vc);
        controller.Name = title;


        controller.PresentPreview(true);
    }
}

public class MyInteractionDelegate : UIDocumentInteractionControllerDelegate
{
    UIViewController parent;

    public MyInteractionDelegate(UIViewController controller)
    {
        parent = controller;
    }

    public override UIViewController ViewControllerForPreview(UIDocumentInteractionController controller)
    {
        return parent;
    }
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接