How to size UIWebView in a UIModalPresentationFormSheet? - ios

I tried every permutation known to mankind and I cannot get a UIWebView to size properly in a modal form view for the iPad. The web page still displays the full iPad portrait width of 768 points.
Where and how do I tell UIWebView to display width 540 points?
I thought 'scalesPageToFit' is supposed to do this but it does not work.
I tried setting the web view to the size of form view which is 540 x 576 with navigation and status bar. Height is irrelevant though.
I tried adding a UIWebView to a UIView in a storyboard with all resizing set. I then removed the UIWebView and added it programmatically.
- (void)viewDidLoad
{
[super viewDidLoad];
CGRect aFrame = self.view.frame;
if (IS_IPAD)
{
aFrame = CGRectMake(0, 0, FORM_VIEW_WIDTH, FORM_VIEW_HEIGHT);
}
_webView = [[UIWebView alloc] initWithFrame:aFrame];
[_webView setOpaque:NO];
[_webView setDelegate:self];
[_webView setScalesPageToFit:YES];
self.view = _webView;
...
}
I also tried loading in viewDidAppear (in addition to viewDidLoad, viewWillAppear, etc.)
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
[[self webView] setFrame:CGRectMake(0, 0, FORM_VIEW_WIDTH, FORM_VIEW_HEIGHT)];
[[self webView] setScalesPageToFit:YES];
NSURL *webURL = [NSURL URLWithString:#"http://www.google.com"];
NSURLRequest *request = [NSURLRequest requestWithURL:webURL cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:5.0f];
[[self webView] loadRequest:request];
}
Any recommendations appreciated.

This keeps happening to me. I post a lengthy question, then two seconds later I find the answer.
Anyway, someone posted a solution to this problem here:
UIWebView -- load external website, programmatically set initial zoom scale, and allow user to zoom afterwards
However, I altered the script to set the width to the iPad width:
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
NSString* js =
#"var meta = document.createElement('meta'); "
#"meta.setAttribute( 'name', 'viewport' ); "
#"meta.setAttribute( 'content', 'width = 540px, initial-scale = 1.0, user-scalable = yes' ); "
#"document.getElementsByTagName('head')[0].appendChild(meta)";
[[self webView] stringByEvaluatingJavaScriptFromString: js];
}
Note the portion 'width = 540px, initial-scale = 1.0, user-scalable = yes'
I changed the width to the form width and scale to 1.0.
I also need to handle iPhone, and so I will make adjustments for that next. I think this fix may only be needed for the iPad form view.
UPDATE: Answer is short-lived. If I load any other page the sizing is thrown off again. Back to square one.
SOLUTION: This view will only need to access a few web pages that I developed. Therefore, I updated the few web pages that I need to access by adding the meta tag to each of them. I only need to access these pages and so this is the solution I plan to go with. This means I need to set the viewport width to 540px only for iPad somehow. I will figure that out next.
<meta name="viewport" content="width:540, initial-scale=1, maximum-scale=1">

Related

UIWebview full screen size

I am trying to display a pdf in a UIWebview using the entire screen of the device, except the status bar and top bar. So far, I created IBOutlet UIWebview *webview; in my .h file. I connected the webview in my storyboard and wrote the following code in my .m file:
- (void)viewDidLoad
{
[super viewDidLoad];
webview = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, 320, 460)];
webview.scalesPageToFit = YES;
webview.autoresizesSubviews = YES;
webview.autoresizingMask=(UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth);
[webview setBackgroundColor:[UIColor clearColor]];
NSString *path = [[NSBundle mainBundle] pathForResource:#"Chart" ofType:#"pdf"];
NSURL *targetURL = [NSURL fileURLWithPath:path];
NSURLRequest *request = [NSURLRequest requestWithURL:targetURL];
[webview loadRequest:request];
[self.view addSubview:webview];
}
The pdf displays correctly in my 3.5 inch, but in my 4 inch display, the bottom is cut off. This is even more noticeable when I rotate to landscape view. About 33% of the screen is not used at all. How can I fix this? I know it must have something to do with the UIWebview dimensions in my code above (webview = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, 320, 460)];), but is there a better way to simply display the webview on the entire screen, without specifying a specific screen height and width?
One cheap thing to do would be to set the frame of the webview in overriding - (void)viewDidLayoutSubviews
- (void)viewDidLayoutSubviews {
webView.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
}
the advantage of doing it here, is that the size and orientation of the view has already been determined here, and you can get an accurate measurement.
Using Auto Layout
Be sure the "Constrain to margins" option is not checked, all constrain values are 0:
And your webview is a first child of your main view:
Without Auto Layout
Change webview autosizing connections:
After a lot of headache dealing with compatibility of iOS6, 7 and different screen sizes, I use this one:
- (void)viewDidLoad {
[super viewDidLoad];
self.webview = [[UIWebView alloc] init];
// More setting your webview here
// Set webview to full screen
self.view = self.webview;
[self.webview loadRequest:aRequest];
}
I was able to get this working correctly for me by selecting the Reset to Suggested Constraints under the Resolve Auto Layout Issues menu.

how to fit 255 width table in UIWebView

I am trying to view the web-page in UIWebView in iOS. Below is the link I am trying to view.
http://almaktab.com/forms/flight.html
The problem is width of table is 255px and when I open this in webview, it covers only half screen which looks odd. Below is the code I am using for viewing web page in UIWebView.
-(void)openWebPage:(NSString*)address {
NSURL*url=[NSURL URLWithString:address];
NSURLRequest*request=[NSURLRequest requestWithURL:url];
myWebView.scalesPageToFit = YES;
myWebView.autoresizingMask = UIViewAutoresizingFlexibleWidth;
myWebView.contentMode = UIViewContentModeScaleAspectFit;
myWebView.autoresizesSubviews = YES;
[myWebView loadRequest:request];
[myWebView setAutoresizingMask:(UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth)];
}
I want to stretch this webview so that form can fit for whole page.
I know solution for this would be width=100%, but client don't want to do any changes in their website now. Client asked to do changes in iOS only.
Any idea how this can be get done.
Screenshot (myWebView.scalesPageToFit = YES;)
Screenshot (myWebView.scalesPageToFit = NO;)
Try:
webView.scalesPageToFit = NO;
And in webViewDidFinishLoad:
[view stringByEvaluatingJavaScriptFromString:#"document.getElementById('table3').width='100%'"];
You should have not use AutoresizingMask.

Displaying a .doc file with custom margins

This piece of code dispalys a document in a scrollable UIWebView:
- (void)viewDidLoad
{
[super viewDidLoad];
_myWebView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, 320, 760)];
NSURL *myUrl = [NSURL URLWithString:#"http://pathToDoc/myDoc.doc"];
NSURLRequest *myRequest = [NSURLRequest requestWithURL:myUrl];
[_myWebView loadRequest:myRequest];
[self.view addSubview:_myWebView];
}
This works fine except when trying to display a landscape document which has been created with custom margins which are smaller than standard.
The result is the contents of the right-hand edge of the document are chopped off.
Is there any way to display the whole document?
Well, after several months of frustration with this (can barely believe how long it took!), it turned out to need only a single line of code to solve the problem.
_documentWebView.autoresizingMask = UIViewAutoresizingFlexibleWidth;
Add that just after the last line and suddenly all is well in the world.

Generating thumbnails of local webpages on iOS

I have a set of local HTML pages that I would like batch generate thumbnails for on the fly (I only want to show the thumbnails, not the full web pages). This is the way I'm accomplishing this:
NSString* path = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:link];
NSURL* url = [NSURL fileURLWithPath:path];
NSURLRequest* request = [NSURLRequest requestWithURL:url];
UIWebView* webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, 725, 1004)];
webView.delegate = cell;
[webView loadRequest:request];
[self.view addSubview:webView]; // doesn't work without this line, but then the UIWebView is onscreen
Then in the delegate:
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
[self performSelector: #selector(render:) withObject: webView afterDelay: 0.01f];
}
- (void) render: (id) obj
{
UIWebView* webView = (UIWebView*) obj;
CGSize thumbsize = CGSizeMake(96,72);
UIGraphicsBeginImageContext(thumbsize);
CGContextRef context = UIGraphicsGetCurrentContext();
CGFloat scalingFactor = thumbsize.width/webView.frame.size.width;
CGContextScaleCTM(context, scalingFactor,scalingFactor);
[webView.layer renderInContext: context];
UIImage *resultImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
self.thumbnail.image = resultImage;
}
Here are my questions:
1) Is my general approach correct? Is this the most efficient way to batch process thumbnails of webpages on the fly?
2) I want to be able to render the thumbnail from an offscreen UIWebView, but the webViewDidFinishLoad: doesn't get called unless I add the UIWebView to the view hierarchy. Is there a way I can avoid this?
3) If I attempt to capture an image of the UIWebView in webViewDidFinishLoad:, I get a blank image. I have to put an artificial delay for the capture to work. Any way around this?
Thanks!
1) This seems to be the only way I know of to do this, without relying on a third party server-based API (if one exists)
2) You can draw the UIWebView off-screen by setting its frame to a position that is off-screen. For example 320,568,725,1008. I haven't tested it, but you should even be able to set the view as a 1 x 1px frame.
3) I imagine this is because when webViewDidFinishLoad: is called, a SetNeedsDisplay() has been called on the web view, but the web view has not yet redrawn itself. I'm not sure about a way around this.

ipad orientation problem with webview

I'm creating ipad application. It has just 2 views.
One view has few buttons. On click of a button it will open another view. This view has a webview which shows PDF file. I'm using the following code to show the PDF
- (void)viewDidLoad {
[super viewDidLoad];
NSString* path = [[NSBundle mainBundle] pathForResource:#"mypdf" ofType:#"pdf"];
[webView loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:path]]];
}
Now the problem is that, when I change the orientation, whole view is rotating, but the pdf is being disturbed and not rendering properly. how can I avoid this?
As the PDF is having 250 pages, reloading the whole PDF on willRotateToInterfaceOrientation and didRotateFromInterfaceOrientation will delay the display.
How to avoid this situation.
There has to be a way a better to avoid/fix this, for now i just read the y scroll position with:
int scrollPosition = [[webView stringByEvaluatingJavaScriptFromString:#"window.pageYOffset"] intValue];
Then reloaded the document in:
(void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation;
and then set the scroll position again with:
[webView stringByEvaluatingJavaScriptFromString: [NSString stringWithFormat:#"scrollTo(0,%d)",scrollPosition];

Resources