How to set initial zoom of PDF in UIWebView - objective-c

I have an app that only supports portrait mode and a local PDF file in landscape orientation that I am loading into a fullscreen UIWebView. When I open the view, the pdf is displayed to full width, but I would like to display it full height (so that actually only a part of it is visible initially).
this is what I currently have:
- (void)viewDidLoad
{
[super viewDidLoad];
mapView.delegate = self;
NSString *path = [[NSBundle mainBundle] pathForResource:#"plan" ofType:#"pdf"];
NSURL *targetURL = [NSURL fileURLWithPath:path];
NSURLRequest *request = [NSURLRequest requestWithURL:targetURL];
[mapView loadRequest:request];
}
scalesPageToFit is set to YES in Storyboard, as I would like users to be able to change the zoom level after initially loading the pdf.
contentMode is set to scaleToFill (in Storyboard, too)
I've tried to play around with these values and also with setting mapView.scrollView.zoomScale programmatically, but no success so far.
How can I achieve that the PDF is loaded into the UIWebView full height instead of full width?

Related

UIWebView not full screen on rotate

(This looks like a question that has been asked/answered 100 times but I can't get it to work.)
I have a view controller that is story board based. Nested in the main view is a webUIView.
In the controller method viewDidLoad I am simply specifying the URL and setting the view and the webUIView to autoresize:
NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:#"index" ofType:#"html" inDirectory:#"web_app_build"]];
NSURLRequest *request=[NSURLRequest requestWithURL:url];
[_browser loadRequest:request];
self.view.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
self.view.autoresizesSubviews = TRUE;
self.browser.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
I also added this method
-(void)viewWillLayoutSubviews {
[super viewWillLayoutSubviews];
self.browser.frame = self.view.bounds;
}
When I rotate the device the webUIView rotates, but doesn't resize to fill the screen. I did try writing my own rotation handler but this seems the entirely wrong way to do that.
I did read the apple doc on the topic. How can I make the webUIview resize to fit the display size on rotate?
The issue you're having is that self.view.bounds has most likely not been adjusted yet to the bounds after rotation. Set a breakpoint to check this out. I would move the code to viewDidLayoutSubviews: instead. If you don't need to support versions of iOS before iOS 6, I'd strongly recommend looking into using Auto Layout for your view objects. It will make your life a lot easier.

How to align UIWebView video (from online DB) to center : Xcode

I am displaying a video to my UIWebView from an online Database as:
//-------- Creating webview --------//
UIWebView *webview=[[UIWebView alloc]initWithFrame:CGRectMake(10, 145, 300,190)];
webview.scalesPageToFit = YES;
webview.backgroundColor = [UIColor redColor];
webview.opaque = NO;
[self.view addSubview:webview];
//-------- Loading Video --------//
NSURL *fileURL = [NSURL URLWithString:
[NSString stringWithFormat:#"http://mysite.org/myProject/sample.php?name=123746"]];
NSURL *nsurl=fileURL;
[webview loadRequest:nsrequest];
It is working great. But the video looks like below:
So, Is there is any way to align the video to center of the web view? Anyone please help. thanks.
3 Options.
If the video will always be that size, make the UIWebView just slightly bigger and center that.
Use [webView stringByEvaluatingJavaScriptFromString:#""] to pass in javascript to adjust the css to center it.
If its a webpage you control, add css to the HTML to center the video div in the window
as I was not able to find any good inbuilt solution using Xcode, I have solved the issue with the help of backend and HTML iframes. I've saved the exact url in a data base and using HTML <iframe>, I loaded the video in the webview with tag to the iframes.

How to change WebView's webpage position? OSX

Let's say I have a WebView that loads up twitter.com.
Here's what appears in my WebView:
And here's what I want to show:
How can I change the default Upper Left Corner position of website on the WebView to display another position of the same website?
I get it to move using this (you need to set the frameLoadDelegate):
-(void)webView:(WebView *)sender didFinishLoadForFrame:(WebFrame *)frame
{
if (frame == self.myWebview.mainFrame)
{
DOMDocument *mainDoc = [self.myWebview.mainFrame DOMDocument];
DOMNodeList *list = [mainDoc getElementsByClassName:#"front-signin js-front-signin"];
DOMNode *loginElement = [list item:0];
NSRect myRect = loginElement.boundingBox;
[self.myWebview stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:#"window.scrollTo(%d,%d);", (int)myRect.origin.x, (int)myRect.origin.y]];
}
}
"front-signin js-front-signin" being the name of the class used in the source html of www.twitter.com.
So it is failure prone to rely on that which could change at any time.
There is also in this case the header that doesn't move. So either account for it and display it, or move your view up the size of this header and hide it.
You need to adjust the scrollTo call to center it in your view.
And you might want to consider fading the view once it's loaded, cause the scroll is kind of jerky, and not very osx like.
But apart from this, it could work ;-)
You could execute javascript in the UIWebView:
[webView stringByEvaluatingJavaScriptFromString:#"window.pageYOffset=200;"];
This should scroll the window down 200 pixels.
You can use the following on OS X:
[webView stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:#"window.scrollTo(%f, %f)", xPos, yPos]];
You can also play with the webview's scrollView contentOffset.
UIWebView * webView = [[UIwebView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
[webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:#"http://www.twitter.com"]]];
[webView.scrollView setContentOffset:CGPointMake(50, 60)];
[webView release];

Mac App WebView background

How is it possible to set the an image as background of the WebView in a Mac App? Because the WebView has that "bouncing" effect, if you scroll further down than the content, the background is usually just white. Is it possible to set a Background-image like it is possible for the UIWebView?
EDIT: The alternate way would be to completely deactivate the bouncing-effect of the webView.
If you are creating this webview programically then create an image then create the webview. Make sure their positions (x,y) and sizes (height, width) are the same.
If you are doing this through IB put down an image (litterally just drag an image) then put the webview on top of it.
Here is a way I found that stops the bouncing:
id sub = self.web.subviews[0];
if ([sub isKindOfClass:[WebFrameView class]])
[sub setAllowsScrolling:NO];
The class check may not be needed, but I like to make sure.
We can add background image in WebView using cocoa. to do this we need to add css for body tag.
Create a webview:
WebView *webView;
Fetch image form bundle path:
NSString *path = [[NSBundle mainBundle] pathForResource:#"my-bg" ofType:#"jpg"];
write Css for body tag.
NSMutableString *htmlBuilder = [[NSMutableString alloc] init];
[htmlBuilder appendString:#"body {"];
[htmlBuilder appendString:[NSString stringWithFormat:#" background-image: url('file://%#');",path]];
[htmlBuilder appendString:#"}"];
Load this image on webview
[[webView mainFrame] loadHTMLString:htmlBuilder baseURL:nil];
[webView setDrawsBackground:NO];
Your image will be added in background on webview.

passing user from UITableView to UIWebView based on selection

I'm trying to pass the user on to the interface based on their cell selection in the UITableView, ie, if the user selects cell 1, they are taken to view model 2, containing UIWebView. UIWebView then displays local file, cell1.html.
Currently, I've manage to get placeholder using:
selectedCellText.text = selectedCell;
to display the name of the cell selected. How do I get it to directly pass to the UIWebView, stick UIWebView in the interface and link it using:
UIWebView *myWebView = [[UIWebView alloc] initWithFrame:frame];
NSBundle *mainBundle = [NSBundle mainBundle];
NSString *stringUrl = [mainBundle pathForResource:#"selectedCell" ofType:#"html"];
NSURL *baseUrl = [NSURL fileURLWithPath:stringUrl];
NSURLRequest *urlRequest = [NSURLRequest requestWithURL:baseUrl];
[myWebView loadRequest:urlRequest];
My other issue is that some of the cell names have spaces in them, and for simplicity, I'd like to ensure that there are no spaces (actually, will it even work with spaces in the name, I assume with %20 ?
Thanks
Fixed by flowing table -> html page