UIWebView not full screen on rotate - cocoa-touch

(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.

Related

How to set initial zoom of PDF in UIWebView

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?

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];

My UITabBar doesn't show up

Okay guys, maybe you can help me out with this one. I'm about ready to pull my hair out.
Recently I decided to upgrade my app and make it look better, and with that I wanted to move it into full support for iPad platforms as well. For a while everything worked great. Just press copy MainWindow.xib for iPad, add the views that I used on the iPhone configurations, and everything should be great, but that didn't work too well. Take a look:
Here is the iPhone screenshot:
Here is the iPad screenshot:
Where's the tab bar? I don't understand! I added the initial view when I was first putting it together, but when I linked all of the IBOutlets to the proper pieces, the tab bar no longer shows up.
Screenshot of IB:
Tab Bar properties:
Tint: A bluish color
Image Tint: A goldish color
Mode: Scale to fill
Tag: 0
User Interaction Enabled: (Checked)
Multiple Touch: (Unchecked)
Alpha: 1
Opaque: (Checked)
Hidden (Unchecked)
Clears Graphic Context: (Checked)
Clip Subviews: (Unchecked)
Autoresize Subviews: (Checked)
Stretching: (x,y,w,h):(0,0,1,1)
The viewController.h file is a delegate for UITabBar, UITextField, and UITextView
ViewDidLoad (bar is the IBOutlet for the tab bar):
- (void)viewDidLoad
{
[super viewDidLoad];
[self playMovieIntro];
NSURL *url = [NSURL URLWithString:#"http://www.faithlifefellowship.us/Audio/Sermons/NewSermonBanner.png"];
NSData *data = [NSData dataWithContentsOfURL:url];
UIImage *image = [UIImage imageWithData:data];
if(!image == NULL)
{
NewSermonBanner.image = image;
}
series = [[Series alloc] init];
SeriesName = #"";
NSRange range = [[[UIDevice currentDevice] name] rangeOfString:#"iPad"];
int i = 0;
if(range.location != NSNotFound)
i = 1;
bar.selectedItem = hometab;
//set delegates
[bar setDelegate:self];
[personalName setDelegate:self];
[personalEmail setDelegate:self];
[content setDelegate:self];
[prContent setDelegate:self];
[prName setDelegate:self];
[prEmail setDelegate:self];
}
I'm stumped. If you have any ideas or need any more information, let me know.
Thanks!
I'm going to give you a few things I'm getting in order to fix this. It will be tons easier if you could upload the source code for me/us to download and be able to pinpoint the problem.
Sometimes (I can't remember exactly when) I've had my navigation bar not show up because it was missing a connection.
Make sure you are not hiding the tab bar anywhere in code, though it doesn't seem to be the case since it shows up on iPhone.
Otherwise I'm gonna take a guess and say it's something in the NIB. Here are some things you can try:
Check all your connections to outlets
Make sure your objects in the NIB are of the correct class
Verify that the tab bar's "hidden" property is not check in Interface Builder
Compare and verify all the structure of the NIB file between iPhone and iPad
These are just some ideas :) again if you can post the code it would be fantastic.
Let us know how it goes,
Felipe
I had this problem with a app that support both iPhone and iPad. Make sure 'is initial view controller' is checked for the UITabBarController when you examine the view controller using the object inspector. When you do this, xcode will display a 'inbound' arrow on the left side of the view controller if you're using storyboards.

Setting NSBox's setContentView with NSImageView not working

I have an NSBox set up in my main view that accepts drag and drop. We store the URL into str. We then load the image and add it to the content view of NSBox.
imageView = [[NSImageView alloc] initWithFrame:CGRectMake(0, 0, 390, 150)];
NSURL *imageURL = [NSURL URLWithString:str];
NSImage *image = [[NSImage alloc] initByReferencingURL:imageURL];
[imageView setImage:image];
[self setContentView:imageView];
However, this doesn't do anything. The image is not displayed in the nsbox.
Another oddity. At first I was trying to add the NSImageView via interface builder, but the only image container they had was IKImageView... Is this odd? Isn't NSImageView more ubiquitous? I mainly develop on iOS, so I have the version from the ios developer site.
Any thoughts?
Edit: I should also add, when I was using IKImageView in IB, the image would show up.
Edit2: I've also tried just taking the initWithFrame out and replacing it with just init, no go.
The problem was that I was using initByReferencingURL to get the NSImage. This doesn't work for local files, so instead I used initByReferencingFile and everything worked out well!

To Convert HTML doc to image in cocoa

is it possible to convert the HTML page to image in cocoa?
Actually i have created the complete view in the HTML and now i want to convert the whole html preview to the image (any jpeg or png etc.).
I couldn't find any resource or sample on the web, which provides some sort of help on my above queries.It's highly appreciated if someone could share his wisdom on how I can achieve this.
Thanks in advance..
First off, I'd like to thank sergio... his answer got me started but I thought I'd share some of the code that I didn't find obvious that I had to write to make it work:
Here's how to make a thumbnail for a page without ever having it displayed:
// Your width and height can be whatever you like, but if you want this to render
// off screen, you need an x and y bigger than the superview's width and height
UIWebView* webView = [[UIWebView alloc] initWithFrame:CGRectMake(largerScreenDimension, largerScreenDimension, largerScreenDimension, largerScreenDimension)];
[self.view addSubview:webView]; // UIWebViews without an assigned superview don't load ever.
webView.delegate = self; // or whoever you have implement UIWebViewDelegate
webView.scalesToFit = YES; // This zooms the page appropriately to fill the entire thumbnail.
[webView loadRequest:[NSURLRequest requestWithURL:url]];
Then implement this in your delegate:
- (void)webViewDidFinishLoad:(UIWebView *)webView {
UIGraphicsBeginImageContext(webView.bounds.size);
[webView.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *webViewImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
NSData *thumbnailData = UIImagePNGRepresentation(webViewImage);
[webView removeFromSuperview];
}
Finally, to display this thumbnail you'll need something like:
thumbnailImageView.image = [UIImage imageWithData:thumbnailData];
As a bonus thing I'll mention, I wanted multiple thumbnails to be generated at once. I found using objc_setAssociatedObject() and objc_getAssociatedObject() to be very helpful with keeping track of which webView was loading which thumbnail. Going into detail on how that worked is beyond the scope of this question, though.
You can draw your view in an image context, like this:
UIWebView* view = ...
....
UIGraphicsBeginImageContext(view.bounds.size);
[view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
NSData *imagedata = UIImagePNGRepresentation(viewimage);
NSString *encodedString = [imageData base64Encoding];
Another option would be using Quartz PDF engine to create a PDF.