UIWebView 在切换到横向时不改变方向
我正在制作这样的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
旋转是由视图控制器而不是视图决定的。 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.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.