You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Xamarin.Forms iOS中禁用QLPreviewController的标题菜单?

解决iPad上QLPreviewController导航栏标题弹出菜单的问题

核心思路

iOS更新后,QLPreviewController的导航栏标题默认会触发包含「Open In」的操作菜单,要禁用该功能,可通过自定义导航栏标题视图或拦截导航栏交互实现。

具体实现(Xamarin.Forms iOS项目)

方法1:替换导航栏标题为自定义视图

用自定义UILabel替换系统默认标题视图,点击标题时不会触发系统菜单:

public class CustomQLPreviewController : QLPreviewController
{
    public override void ViewDidLoad()
    {
        base.ViewDidLoad();
        
        // 创建自定义标题标签
        var titleLabel = new UILabel
        {
            Text = "文件预览", // 替换为你的实际标题文本
            Font = UIFont.SystemFontOfSize(17, UIFontWeight.Semibold),
            TextColor = UIColor.Black,
            TextAlignment = UITextAlignment.Center
        };
        
        // 设置为导航栏标题视图
        NavigationItem.TitleView = titleLabel;
    }
}

之后在项目中使用这个自定义的CustomQLPreviewController替代原生QLPreviewController即可。

方法2:拦截导航栏标题的触摸交互

如果不想替换标题视图,可通过自定义UINavigationBar拦截标题区域的触摸事件:

public class CustomNavigationBar : UINavigationBar
{
    public override UIView HitTest(CGPoint point, UIEvent uievent)
    {
        var hitView = base.HitTest(point, uievent);
        // 判断点击对象为导航栏标题视图时,返回null阻止交互
        if (hitView != null && hitView is UILabel && hitView.Superview is UINavigationItemView)
        {
            return null;
        }
        return hitView;
    }
}

再在自定义QLPreviewController中替换导航栏:

public class CustomQLPreviewController : QLPreviewController
{
    public override void ViewDidLoad()
    {
        base.ViewDidLoad();
        NavigationController.NavigationBar = new CustomNavigationBar();
    }
}

补充注意事项

  • 确保已正确关闭QLPreviewController原生的「Open In」按钮:iOS 11+可设置QLPreviewController.AllowsExporting = false,旧版本需在数据源方法中禁用导出选项。
  • 不同iOS版本的导航栏结构可能有细微变化,方法2的HitTest判断逻辑需根据测试结果调整。

内容的提问来源于stack exchange,提问作者Pelin Konaray

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.06 08:35:26