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

发布于 2024-11-07 11:06:31 字数 2000 浏览 0 评论 0原文

我正在制作这样的 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;
    }
}

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

I am making a UIWebView like this (case 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;
    }
}

The problem is that, when I switch to landscape mode, it doesn't change its orientation. What am I doing wrong? Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

邮友 2024-11-14 11:06:31

旋转是由视图控制器而不是视图决定的。 UIViewController 的默认实现 shouldAutorotateToInterfaceOrientation: 仅对于纵向返回 YES;您必须创建 UIViewController 的子类,重写该方法,并使用该子类而不是普通的 UIViewController。

Rotation is determined by the view controller, not the view. UIViewController's default implementation of shouldAutorotateToInterfaceOrientation: returns YES only for portrait; you will have to create a subclass of UIViewController, override that method, and use that subclass instead of a stock UIViewController.

嘦怹 2024-11-14 11:06:31

UIWebView 有一种非常简单的方法,从 CSS3 媒体查询的角度来确定方向。

基本上,如果宽度大于高度,则将其视为横向,如果高度大于宽度,则将其视为纵向。由于您的网页视图固定为 320 x 400,因此它将始终返回纵向。

UIWebView has a very naive method of determining orientation from the standpoint of e.g. CSS3 media queries.

Basically if it's wider than it is tall, it's considered landscape, and if it's taller than it is wide, it's considered portrait. Since your web view is fixed at 320 x 400, it will always return portrait.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文