【问题标题】:How can i use a pdf as a image in storyboard?如何在情节提要中使用 pdf 作为图像?
【发布时间】:2026-02-06 07:55:02
【问题描述】:

我想展示一张图片。 我知道的简单方法是:在界面上放置一个图像视图,然后选择属性检查器并输入我的文件名(对于放入我的项目中的文件)。这适用于我的图片的 png 版本。

但我想在这里使用 pdf 文件。可能吗?在屏幕上显示我的 pdf 文件的最佳方式是什么?

【问题讨论】:

  • 您可以使用网络视图。
  • 如何将 png 连接到 web 视图?
  • PDF 需要从代码中设置...
  • 我该怎么做?
  • 使用 PSPDFKit,pspdfkit.com

标签: ios xcode pdf


【解决方案1】:

显示 PDF 的最佳选择是在 Web 视图中。像添加图像视图一样添加 Web 视图并将插座连接到它。然后,在代码中,当视图将被显示时,调用:

NSString *filePath = [[NSBundle mainBundle] pathForResource:@"MyPDF" ofType:@"pdf"];
[self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:filePath]]];

【讨论】:

  • 有没有办法让图片随网页视图缩放?
  • 看看scalesPageToFit
【解决方案2】:

除了网页视图,如果你想呈现一个 PDF 并让用户与之交互,那么你可以使用文档交互控制器。见Document Interaction Programming Topics

例如:

- (IBAction)didTouchUpInsidePDFButton:(id)sender
{
    NSURL *url = [[NSBundle mainBundle] URLForResource:@"sample" withExtension:@"pdf"];
    UIDocumentInteractionController *controller = [UIDocumentInteractionController interactionControllerWithURL:url];
    controller.delegate = self;

    [controller presentPreviewAnimated:YES];
}

- (UIViewController *) documentInteractionControllerViewControllerForPreview: (UIDocumentInteractionController *) controller
{
    return self;

    // or if you want to push to the PDF preview, and the current view controller 
    // already has navigation controller, you can:
    //
    // return self.navigationController;
}

【讨论】:

    【解决方案3】:
           public override void ViewDidLoad()
        {
            base.ViewDidLoad();
            if (ViewModel == null)
                return;
            FirstViewModel.globalpref = "Not set yet";
            FirstViewModel.islogin = "no";
            UITabBar.Appearance.SelectedImageTintColor = UIColor.Purple;
            UITabBar.Appearance.BackgroundColor = UIColor.FromRGB(193, 168, 210);
            UITabBar.Appearance.BarTintColor = UIColor.FromRGB(193, 168, 210);
            NewsViewModel newsViewModel = (NewsViewModel)Mvx.IocConstruct(typeof(NewsViewModel));
            VideoHomeViewModel videoViewModel = (VideoHomeViewModel)Mvx.IocConstruct(typeof(VideoHomeViewModel));
            MatchViewModel matchViewModel = (MatchViewModel)Mvx.IocConstruct(typeof(MatchViewModel));
            InterestsViewModel iViewModel = (InterestsViewModel)Mvx.IocConstruct(typeof(InterestsViewModel));
            LocationsViewModel lViewModel = (LocationsViewModel)Mvx.IocConstruct(typeof(LocationsViewModel));
            TicketViewModel ticketViewModel = (TicketViewModel)Mvx.IocConstruct(typeof(TicketViewModel));
            var viewControllers = new UIViewController[]
            {
                CreateTabFor("", "News",  newsViewModel),
                CreateTabFor("", "ivideo", videoViewModel),
                CreateTabFor("", "match_center", matchViewModel),
                CreateTabFor("", "ivideo", ticketViewModel),
            };
            ViewControllers = viewControllers;
            SelectedViewController = ViewControllers[0];
        }
        private int _createdSoFarCount = 0;
        private UIViewController CreateTabFor(string title, string imageName, IMvxViewModel viewModel)
        {
            var controller = new UINavigationController();
            controller.NavigationBar.Translucent = true;
            controller.NavigationBar.BackgroundColor = UIColor.Clear;
            controller.NavigationBar.TintColor = UIColor.White;
            controller.NavigationBar.SetBackgroundImage(new UIImage(), UIBarMetrics.Default);
            controller.NavigationBar.ShadowImage = new UIImage();
            controller.NavigationItem.Title = "test";
            var screen = this.CreateViewControllerFor(viewModel) as UIViewController;
            SetTitleAndTabBarItem(screen, title, imageName);
            controller.PushViewController(screen, true);
            return controller;
        }
        private void SetTitleAndTabBarItem(UIViewController screen, string title, string imageName)
        {
            screen.Title = title;
            screen.TabBarItem = new UITabBarItem(title, UIImage.FromBundle(imageName),
                                                 _createdSoFarCount);
          //  UIImage.FromBundle("CameraIcon").Scale(new CoreGraphics.CGSize(this.TabBar.Bounds.Height - 20, this.TabBar.Bounds.Height - 20));
            _createdSoFarCount++;
        }
    

    这仅在 Mvvmcross.iOS 中测试过

    【讨论】:

      最近更新 更多