UIWebview fitting to screen but not zooming in iphone programming - iphone

I am working on uiwebview and it is supposed to load dynamic urls coming from the server.
i want to show it fitting to the iPhone screen size in my iPhone application and also it should zoom in and out.
Code i am using for it is:
m_pView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 430)];
m_pView.backgroundColor = [UIColor blackColor];
self.view = m_pView;
UIWebView *aWebView = [[[UIWebView alloc] initWithFrame:CGRectMake(0, 1, 320, 400)] autorelease];
aWebView.dataDetectorTypes = UIDataDetectorTypeNone;
aWebView.delegate = self;
NSString* url = [NSString stringWithFormat: #"http://%#",[m_URL stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
aWebView.scalesPageToFit = YES;
aWebView.autoresizesSubviews = YES;
aWebView.frame = self.view.bounds;
NSURL *aURL = [NSURL URLWithString: url];
NSURLRequest *aRequest = [NSURLRequest requestWithURL:aURL];
//load the index.html file into the web view.
[aWebView loadRequest:aRequest];
[m_pView addSubview:aWebView];
self.m_webView = aWebView;
Please let me know how to achieve webview fitting to the iphone screen size without loosing zoom in/out feature.
Thanks in advance.

Use following code
yourWEbView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
yourWEbView.contentMode = UIViewContentModeScaleAspectFit;

problem might be in this line:
aWebView.frame = self.view.bounds;
Why you're setting bounds of self.view while adding webview to m_pView?

Related

Set Background Image on GADBannerView

I wanna ask how to set background image while loading ads until it shows the ads.
AdMob = [[GADBannerView alloc]init];
AdMob.frame = CGRectMake(0, 0 , GAD_SIZE_300x250.width, GAD_SIZE_300x250.height);
AdMob.backgroundColor = [UIColor grayColor];
AdMob.adUnitID = [GlobalVariable sharedInstance].PID_IOS_PHONE;
AdMob.rootViewController = self;
GADRequest *request = [GADRequest request];
[AdMob loadRequest:request];
you can set Image as a background using bellow piece of code:-
AdMob.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"Background.png"]];

Centering UIWebView content with padding

I have a UIView which loads a html5 banner. I get padding on each side of the banner which is to be expected since the banners width is shorter than the UIWebView. However.. The padding on each side is a lot bigger than I should get.
I just want the banner to be centered if the banner is shorter than the UIWebView.
Instead of looking like this: (Where X is banner and O is padding)
|OOXXXXXXOO|
It looks like this:
|OOOOXXXXXX|
Here, if I enable scrolling I can center it like the first example.
How do I fix this?
Code
UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:bannerIdentifier];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"bannerCell"];
UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, cell.contentView.bounds.size.width, cell.contentView.bounds.size.height)];
webView.autoresizingMask = UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleLeftMargin|UIViewAutoresizingFlexibleRightMargin;
webView.userInteractionEnabled = YES;
webView.backgroundColor = [UIColor clearColor];
webView.opaque = NO;
webView.delegate = self;
webView.scrollView.bounces = NO;
webView.scrollView.scrollEnabled = NO;
webView.scalesPageToFit = YES;
NSURL *url = [NSURL URLWithString:#"http://example.com/page.html"];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[webView loadRequest:requestObj];
[cell.contentView addSubview:webView];
}
The thing is UIWebView shows HTML, that means, that if your HTML has to be rendered as you wish - the only correct way to achieve this is to fix loaded HTML (and or css).
You can do it in several ways:
If you have access to the HTML data on server you could tweak it to be rendered correctly in UIWebView.
If you don't have access to the HTML data on server you could use stringByEvaluatingJavaScriptFromString: and fix desired elements in DOM.
You can use stringByEvaluatingJavaScriptFromString: to query the DOM for the width of the desired element using Javascript. You can then resize the webview to fit that size.
This gives you the width:
document.getElementById('YOURID').offsetWidth;

UIWebview scalesPageToFit doesn't work perfectly

I'm using a small UIWebView, and scalesPageToFit doesn't work correctly for some url. (look like it cannot resize at 100%. (but if i zoom out the webview manually(gesture), it work!
view = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 200.0f, 400.0f)];
webViewT = [[UIWebView alloc] initWithFrame:view.bounds];
webViewT.delegate = self;
webViewT.scalesPageToFit = YES;
NSURL* url = [NSURL URLWithString:#"http://www.google.com/"];
[webViewT loadRequest:[NSURLRequest requestWithURL:url]];
Please note that i've found a solution:(below work, BUT I have the time to see a "resize"
- (void) webViewDidFinishLoad:(UIWebView *)webViewT {
if ([theWebView respondsToSelector:#selector(scrollView)])
{
UIScrollView *scroll=[theWebView scrollView];
float zoom=theWebView.bounds.size.width/scroll.contentSize.width;
[scroll setZoomScale:zoom animated:NO];
}
}
Any idea? Thanks!
In Some of cases this types of problem Generated. At that time java script use for control of Zoom of UIWebView, use following code.
NSString *jsCommand = [NSString stringWithFormat:#"document.body.style.zoom = 1.5;"];
[webLookupView stringByEvaluatingJavaScriptFromString:jsCommand];
Other way is
Refer this Answer UIWebView does not scale content to fit

add running videos in a scrollView

I have created a scrollView and need to add some running videos to it.
I did the following thing:
scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
scrollView.pagingEnabled = YES;
NSURL *fileUrl1 = [[NSURL alloc] initFileURLWithPath:[[NSBundle mainBundle] pathForResource:#"AUDI_anti_crevaison_1" ofType:#"mp4"]];
NSURL *fileUrl2 = [[NSURL alloc] initFileURLWithPath:[[NSBundle mainBundle] pathForResource:#"AUDI_Utilisation_de_la_roue_de_secours_2" ofType:#"mp4"]];
videoPlayer1 = [[MPMoviePlayerController alloc] initWithContentURL:fileUrl1];
videoPlayer2 = [[MPMoviePlayerController alloc] initWithContentURL:fileUrl2];
[self.scrollView addSubview:videoPlayer1.view];
scrollView.contentSize = CGSizeMake(self.view.frame.size.width *2, self.view.frame.size.height);
[self.view addSubview:scrollView];
[scrollView release];
First of all, it seems this line: [self.scrollView addSubview:videoPlayer1] is broken, I cannot add MPMoviePlayer to a UIView.
And second how can I set the frame for each video...the xOrigin and yOrigin?
I would appreciate a complete code with adding .mp4 files to a UIScrollView cause I cannot seem to make it work.
Thank you!:)
You should read the documentation first.
You're trying to add a controller instead of a view that's why it
doesn't work. I see that you have Android experience - it's like the difference between activity and view on android
If you still can't figure it out there's a lot of code for adding videos to views on the web, just search it. Don't see it necessary to add here such a generic code.

UIWebView autorotation weird behavior

I managed to show a UIWebView that shows content from the internet.
But apparently it does not rotate as it should:
http://www.youtube.com/watch?v=VV0BmNkSrvU
I'm using this code:
UIWindow* window = [UIApplication sharedApplication].keyWindow;
if (!window) {
window = [[UIApplication sharedApplication].windows objectAtIndex:0];
}
_webView = [[UIWebView alloc] initWithFrame:rect];
_webView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
NSURL* url = [[NSURL alloc] initWithString:URL]; NSURLRequest* request = [[NSURLRequest alloc] initWithURL:url];
[_webView loadRequest:request];
//Unity's UIView
UIView* subView = [window.subviews objectAtIndex:0];
[url release];
[subView addSubview:_webView];
It seems that this code has not been placed inside a UIViewController. Autorotation works correctly only inside a UIViewController. If you want to manage it outside a UIViewController you must observe all orientation change notifications and then transform your view accordingly.