Reputation: 2670
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];
}
Upvotes: 1
Views: 597
Reputation: 7494
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;
Upvotes: 1
Reputation: 6032
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:
Upvotes: 2