如何在故事板中使用pdf作为图像?

时间:2013-12-14 14:36:39

标签: ios xcode pdf

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

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

3 个答案:

答案 0 :(得分:1)

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

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

答案 1 :(得分:1)

除了Web视图之外,如果要呈现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;
}

答案 2 :(得分:0)

       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中进行了测试