Reputation: 7922
I'd like to fade out a view and display a webview at the end, with the page scaled to fit in the webview. However, when I try to display the webview at the end of the animation via the completion block, the webpage is not scaled to fit properly, but rather is scaled in too much and requires panning to see it all. For example, if I have:
- (void)viewDidLoad {
[super viewDidLoad];
webView = [[UIWebView alloc] init];
webView.scalesPageToFit = YES;
webView.backgroundColor = [UIColor blackColor];
self.view.alpha = 1;
self.view.backgroundColor = [UIColor whiteColor];
}
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
[webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://stackoverflow.com"]]];
// self.view = webView;
[UIView animateWithDuration:2 delay:0.0
options:UIViewAnimationOptionBeginFromCurrentState
animations:^{ self.view.alpha = 0; }
completion:^(BOOL fin){ self.view = webView; }];
}
things don't scale properly in the UIWebView, but if I ditch the animation and just assign self.view = webView
, the page renders as one would expect when scalesPageToFit = YES
. Anyone know what's going on?
Upvotes: 1
Views: 5615
Reputation: 7922
To answer my own question, I think it's due to the webview not having any explicit reference frame to work with initially while it's loading contents, so it can't layout the contents correctly as seen when I suddenly made it the controller's view after the animation.
As a fix, all I had to do was give it a frame in viewDidLoad: webView.frame = self.view.frame;
And I got lucky above; the frame was already set for self.view by the time viewDidLoad:
since it was specified in the nib. If I dynamically generated the views in say loadView:
without setting any frames there, I'd have to defer that frame assignment to viewWillAppear:
. (Wasted a little more time reminding myself of that the hard way!)
Upvotes: 2