UIWebView在切换到横向时不会改变方向

时间:2011-05-15 02:11:47

标签: objective-c cocoa-touch ios uiwebview uiviewcontroller

我正在制作这样的UIWebView(案例2):

- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
    CGRect frame = CGRectMake(0, 0, 320, 400);
    SongDetailsTVC *infoVC;
    UIWebView *ytView;
    NSString *htmlVideoHTML, *html;
    UIViewController *youtubeController;
    switch (buttonIndex) {
        case 0:
            [self swapLanguage];
            break;
        case 1:
            infoVC = [[SongDetailsTVC alloc] initWithStyle:UITableViewStyleGrouped];
            [infoVC setSongData:songData];
            [infoVC setTitle:@"Song Info"];
            [[self navigationController] pushViewController:infoVC animated:YES];
            [infoVC release];
            break;
        case 2:

            // HTML to embed YouTube video
            htmlVideoHTML = @"<html><head>\
            <body style=\"margin:0\">\
            <embed id=\"yt\" src=\"%@\" type=\"application/x-shockwave-flash\" \
            width=\"%0.0f\" height=\"%0.0f\"></embed>\
            </body></html>";

            // Populate HTML with the URL and requested frame size
            html = [NSString stringWithFormat:htmlVideoHTML, [songData objectForKey:@"YouTube Link (Value)"], frame.size.width, frame.size.height];

            // Load the html into the webview                        
            ytView = [[UIWebView alloc] initWithFrame:frame];
            [ytView setMediaPlaybackRequiresUserAction:NO];
            [ytView loadHTMLString:html baseURL:nil];

            youtubeController = [[UIViewController alloc] init];
            [youtubeController setView:ytView];
            [youtubeController setTitle:@"YouTube Video"];
            [[self navigationController] pushViewController:youtubeController animated:YES];

            [ytView release];
            [youtubeController release];

            break;
        default:
            break;
    }
}

问题在于,当我切换到横向模式时,它不会改变其方向。我究竟做错了什么?感谢。

2 个答案:

答案 0 :(得分:2)

旋转由视图控制器决定,而不是视图。 UIViewController的默认实现shouldAutorotateToInterfaceOrientation:仅为肖像返回YES;你将不得不创建一个UIViewController的子类,覆盖该方法,并使用该子类而不是股票UIViewController。

答案 1 :(得分:0)

UIWebView有一种非常天真的确定方向的方法,例如CSS3媒体查询。

基本上,如果它比它高,它被认为是风景,如果它比它更宽,它被认为是肖像。由于您的Web视图固定为320 x 400,因此它将始终返回纵向。