0

I am trying to display a pdf in a UIWebview using the entire screen of the device.I have an xcode application that runs on the iPad. I have a scrollview to scroll pdf pages UIWebview is a subview of uiscrollview. The PDF is downloaded from the internet with no problems, Currently, the PDF only uses the top most half of the screen on uiwebview in portrait mode when it is displayed. I need it to span the UIWebview entire view.

enter image description here NSString *strPageURL = [dictPage valueForKey:@"link"];

        strPageURL = [strPageURL stringByReplacingOccurrencesOfString:@"\n" withString:@""];
        strPageURL = [strPageURL stringByReplacingOccurrencesOfString:@"\t" withString:@""];
        strPageURL = [strPageURL stringByReplacingOccurrencesOfString:@" " withString:@""];

        NSString* strFileName = [strPageURL lastPathComponent];
                NSString *strDestFile = [NSHomeDirectory() stringByAppendingPathComponent:[NSString stringWithFormat:@"Documents/%@",strFileName]];

        CGRect rect = CGRectMake(xPos, 0, scrollView.frame.size.width, scrollView.frame.size.height);     
        UIWebView * webView = [[UIWebView alloc] initWithFrame:rect];
        webView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin;
        [webView setContentMode:UIViewContentModeScaleAspectFit];
        [webView setScalesPageToFit:YES];
        webView.backgroundColor = [UIColor whiteColor];
        webView.tag=3000+i;

        BOOL fileExists = [[NSFileManager defaultManager] fileExistsAtPath:strDestFile];

        if(fileExists)
        {

            NSURL *url = [[NSURL alloc] initFileURLWithPath:strDestFile];

            [webView loadRequest:[NSURLRequest requestWithURL:url]];
            [webView setDelegate:self];
            [scrollView addSubview:webView];

        }

2 Answers 2

1

The UIWebView has a UIScrollView built in, so you don't actually need to manage this manually.

You can just load a PDF into the UIWebView and it'll automatically display it in the correct form.

You can do this via your URL Request:

//first create the webview
UIWebView *theWebView = [[UIWebView alloc] initWithFrame:[self.view bounds]];
[theWebView setContentMode:UIViewContentModeScaleAspectFit];
[theWebView setAutoresizingMask:UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth];
[theWebView setScalesPageToFit:YES];

//then load the correct file
NSString *filePath = [[NSBundle mainBundle] pathForResource:YOURFILENAME ofType:YOURFILETYPE];
NSURL *filePathURL = [NSURL fileURLWithPath:filePath];        
[theWebView loadRequest:[NSURLRequest requestWithURL:filePathURL]];

//then add to your view
[self.view addSubview:theWebView];

YOURFILENAME and YOURFILETYPE are obviously to be replaced with your own code.

If it still doesn't resize when it hits landscape, you may have to add some code within the autorotation method to resize manually - though the setAutoresizingMask line should take care of it for you.

Sign up to request clarification or add additional context in comments.

6 Comments

sry,i need display pdf file uiwebview portrait full screen ,i have attached screenshot for your reference
That's exactly what my solution does. [self.view bounds] sets the webview to the size of the full view. alternatively, you could use [[UIScreen mainScreen] bounds] for the frame.
i have posted my code please check it what i am doing wrong? how can i do this?
i want to add UIWebview as a subview of UIScrollview why because of i am getting pdf file(single page ) from feed
What's the purpose of the UIScrollView?
|
0

I think you don't have to use ScrollView. Just add web view. Click on the Web View in your XCode Storyboard (or xib file). In right bar, go to "Attributes Inspector" (see attached screenshot). Check "Scales Page to Fit".

Scales Page to Fit

1 Comment

i am using UIScrollview. i have already set [webView setScalesPageToFit:YES];

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.