开发者

uiwebview youtube starting in landscape thumbnail does resize on rotation

开发者 https://www.devze.com 2023-02-22 05:51 出处:网络
I have navigation controller with a tableview. When you click on one of the cells it pushes on a view with a uiwebview on it. You are taken to a YouTube page.

I have navigation controller with a tableview. When you click on one of the cells it pushes on a view with a uiwebview on it. You are taken to a YouTube page.

When you are on the table view in portrait and click on a cell you see the youtube page in portrait. Changing your orientation the video thumbnail does not refresh. So the thumbnail is smaller. This is fine. I actually prefer it smaller. All the content that would consume 2 lines will then consume 1 line. So in other words everything else adjusts for the new dimensions.

The problem comes in when you start off in landscape. Since the thumbnail doesn't resize on orientation change, changing to portrait mode, the image now goes off the screen, while the rest of the content adjusts correctly.

[webVIew refresh];

does work but it obviousl开发者_运维问答y loads the entire page again. So depending on the connection there will be a flicker or possibly the site will go white until its finished loading (on slower connections).

I also tried load the website in an iframe. I asked a similar question yesterday, this was for local pages i was creating. The answer to that question was to put <meta name='viewport' content='width=device-width; initial-scale=1.0; maximum-scale=1.0;'> in the head. So I tried that with an iframe going and getting the page. That seemed like a dumb hack to begin with, but i was willing to go there. It wouldn't even load the page at all. I guess because the youtube page i'm loading redirects to yet other page. Upon further research it seemed like there were other issues with the iframe such as scrolling.

So my question is how can i: A. Get just the thumbnail to resize/reload on orientation change OR B. Get the thumbnail to load in the dimensions it would load in portrait mode all the time, even if it was started in landscape.


You should override this method in your UIViewController class and do the resizing there:

  • (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration;


I found the answer, maybe someone could explain it or offer a better solution. There was also an addition bug that I had to work out.

  1. I made sure the uiwebview had the delegate set to files owner.
  2. I changed the parent view when it pushed the view onto the form from:

    [self.navigationController pushViewController:controller animated:YES];

to (the important part being the 320, i'm restricting the view to portrait):

[self.navigationController pushViewController:controller animated:YES];
controller.webView.hidden = YES;
controller.webView.frame=CGRectMake(0, 0, 320,367);

3.On the webview i load the url as i always did:

NSURL *url = [NSURL URLWithString:sUrl];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[webView loadRequest:requestObj];

4.In the webviewdidfinishload i now have:

if (self.interfaceOrientation ==
               UIInterfaceOrientationLandscapeLeft || 
               self.interfaceOrientation ==
               UIInterfaceOrientationLandscapeRight) {
        self.webView.frame=CGRectMake(0, 0, 480,227);

    }
    self.webView.hidden = NO;
    [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;

All this would normally have solved my issue, but then i found out that the gdata url i'm getting from youtube service actually gets resolved to a 2nd url. Then i guess youtube changed their url format so it is forwarded to a 3rd url. This means that my didfinishload code was being called before the final url had loaded. To solve this i added:

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
    if ([[[NSString stringWithFormat:@"%@",request.URL] substringWithRange:NSMakeRange(0, 26)] isEqualToString:@"http://m.youtube.com/watch"]) {
        NSString *sUrl=[NSString stringWithFormat:@"%@",request.URL];
        sUrl = [sUrl stringByReplacingOccurrencesOfString:@"http://m.youtube.com/watch?" withString:@"http://m.youtube.com/#/watch?"];

        NSURL *url = [NSURL URLWithString:sUrl];

        NSURLRequest *requestObj  = [NSURLRequest requestWithURL:url];

        [self.webView loadRequest:requestObj];
        return NO;
    }
    //NSLog(@"request:%@",request.URL);
    return YES;
}

Which im sure could be better but basically i tell it to ignore the 2nd url, and make my own change to go to the 3rd url.

0

精彩评论

暂无评论...
验证码 换一张
取 消