Ios – UIWebView: Fit frame to view size, allow scrollowing within that frame on UISplitViewController

iosobjective cuiscrollviewuiviewuiwebview

This problem is driving me nuts.

I have a UIWebView which I am trying to make fill the full view on the right side of a UISplitViewController:

CGSize boundsSize = self.view.bounds.size;
self.webView = [[[UIWebView alloc] initWithFrame:CGRectMake(0, 0, boundsSize.width, boundsSize.height)] autorelease];
self.webView.delegate = self;
self.webView.opaque = NO;
self.webView.backgroundColor = [UIColor redColor];
self.webView.allowsInlineMediaPlayback = YES;
self.webView.dataDetectorTypes = UIDataDetectorTypeAddress | UIDataDetectorTypeLink | UIDataDetectorTypePhoneNumber;
self.webView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleLeftMargin;
[self.view addSubview:webView];

First of all, how do I account for making the frame size fit the view when I don't know the orientation? Can I autoresize to the appropriate size?

Next, when I load my HTML into my webView I want the frame size to stay the same (just like a UIScrollView does), but have that new content be able to be scrolled with that frame. Some content is much longer, some is not. So, I have tried to adjust the frame, but this just makes my frame larger rather than my content size.

- (void)webViewDidFinishLoad:(UIWebView *)aWebView {

    // Calculate the size of our webview
    CGRect frame = aWebView.frame;
    frame.size.height = 1;
    aWebView.frame = frame;
    CGSize fittingSize = [aWebView sizeThatFits:CGSizeZero];
    frame.size = fittingSize;
    aWebView.frame = frame;

}//end

Is this not possible? Do I need to put the webView inside of a UIScrollView and set the contentSize of that to the height of my new content every time I load it into my webView?

Best Answer

I think that is all you need to make this work.

[articleWebView sizeThatFits:CGSizeZero];

Related Topic