Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS

Showing PDF in UIWebView full height

I am loading a landscape PDF into a UIWebView of an app that only supports portrait mode. the PDF is currently displayed full width, but I would like to display it full height ((so that actually only a part of it is visible initially).

I've tried to set zoomScale programmatically, but this doesn't seem to affect display.

How can I achieve that the PDF is loaded into the UIWebView full height instead of full width?

Here's a snippet from the code I am currently using: ``` - (void)viewDidLoad { [super viewDidLoad]; mapView.delegate = self; NSString *path = [[NSBundle mainBundle] pathForResource:@"map" ofType:@"pdf"]; NSURL *targetURL = [NSURL fileURLWithPath:path]; NSURLRequest *request = [NSURLRequest requestWithURL:targetURL]; [mapView loadRequest:request]; }

3 Answers

Hi Roger,

I've been working a year ago on something similar as part of a bigger project (present a PDF in a UIWebView and save/restore the position and zoom status of the pdf) and I might know why your setting of the zoomScale fails :)

The PDF doesn't merely gets loaded into the UIWebView but it also has to be rendered too which takes quite a while. So you need a way to send a message to the UIWebView AFTER the pdf has been fully rendered (before that trying to set the zoomScale will fail)

My solution then was to use a performSelector method with afterDelay to give the UIWebView the time it needs to render the pdf.

Here are some snippets of the code - hope this is of some help for you :)

- (void)viewDidLoad
{
    #if DEBUG
    NSLog(@"%s", __PRETTY_FUNCTION__);
    #endif

    [super viewDidLoad];
    // Do any additional setup after loading the view

    NSString *path = [[NSBundle mainBundle] pathForResource:@"print" ofType:@"pdf"];
    NSURL *targetURL = [NSURL fileURLWithPath:path];
    NSURLRequest *request = [NSURLRequest requestWithURL:targetURL];
    [self.webViewOutlet loadRequest:request];

    // Unsichtbar machen, um den Scrollvorgang zu verstecken
    self.webViewOutlet.alpha = 0.01;
    // Positionierung der PDF
    [self performSelector:@selector(adjust) withObject:nil afterDelay:0.5];

}

- (void)viewWillDisappear:(BOOL)animated
{
    #if DEBUG
    NSLog(@"%s", __PRETTY_FUNCTION__);
    #endif

    // Define Position of PDF
    [super viewWillDisappear:YES];
    AppDelegate* delegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
    delegate.zoomScalePrint = self.webViewOutlet.scrollView.zoomScale;
    delegate.pointPrint = self.webViewOutlet.scrollView.contentOffset;
}

// Positionierung der PDF (wird mit Hilfe der Funktion performSelector in der Methode viewDidLoad mit Verzögerung aufgerufen
- (void)adjust
{
    AppDelegate* delegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
    [self.webViewOutlet.scrollView setZoomScale:delegate.zoomScalePrint animated:NO];
    [self.webViewOutlet.scrollView setContentOffset:delegate.pointPrint animated:NO];
    // Nach erfolgtem Scrollvorgang wieder sichtbar machen
    self.webViewOutlet.alpha = 1;
}

Position data has been saved in AppDelegate (not quite the MVC concept :-) ) hence the

AppDelegate* delegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
delegate.zoomScalePrint = self.webViewOutlet.scrollView.zoomScale;

AppDelegate.h

@interface AppDelegate : UIResponder <UIApplicationDelegate>

@property (strong, nonatomic) UIWindow *window;

@property (nonatomic) float zoomScaleApp;
@property (nonatomic) CGPoint pointApp;

@property (nonatomic) float zoomScalePrint;
@property (nonatomic) CGPoint pointPrint;

@property (nonatomic) long ActiveTabPosition;
@end

AppDelegate.m

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    // Override point for customization after application launch.
    self.zoomScaleApp = 0.0;
    self.pointApp = CGPointMake(0.0, 0.0);
    self.zoomScalePrint = 0.0;
    self.pointPrint = CGPointMake(0.0, 0.0);
    self.ActiveTabPosition = 0;
    return YES;
}

wow - thanks a lot! I will dive straight into your code...

on another note: how did you get your code to be so nicely formatted? I tried with the ``` from the Markdown Cheatsheet but failed miserably.

Updated

double post, sorry

The three single quotes from the Markdown Cheatsheet should work just fine - maybe some space between this quotes and the code could help :)

On german layout keyboards I use the key between the ?/ß key and the delete key + shift.