2

我的应用只是在第 4 代 iPad 上加载 webview。

它具有 768 x 1024 纵向和 1024 x 768 横向视图尺寸。

但是我正在加载的内容是 760 x 1014。

该应用程序支持旋转和缩放。

如果视图已加载,我希望它适合全屏纵向以及带有滚动条的横向。

所以我像这样在标题上添加了一个元标记

<meta name="viewport" id="view" content="width=760, user-scalable=yes">

也在我的sdk上

- (void)viewDidLoad{
mainWebView = [[UIWebView alloc] initWithFrame:self.view.bounds];
mainWebView.delegate = self;

[mainWebView setAutoresizingMask:(UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth)];
mainWebView.scalesPageToFit = YES;
mainWebView.contentMode = UIViewContentModeScaleAspectFit;

for(id subview in mainWebView.subviews) {
    if([[subview class] isSubclassOfClass:[UIScrollView class]]) {
        ((UIScrollView *)subview).bounces = NO;
        ((UIScrollView *)subview).alwaysBounceVertical = NO;
        ((UIScrollView *)subview).alwaysBounceHorizontal = NO;
        ((UIScrollView *)subview).bouncesZoom = NO;
        ((UIScrollView *)subview).maximumZoomScale = 2.0;
        ((UIScrollView *)subview).minimumZoomScale = 1.0;
    }
}
[self.view addSubview:self.mainWebView];
NSURL *url = [NSURL URLWithString:@"http://my app"];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[mainWebView loadRequest:request];
}

连同这些

-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation{
if (interfaceOrientation == UIDeviceOrientationLandscapeLeft || interfaceOrientation == UIDeviceOrientationLandscapeRight) {
    NSLog(@"Rotated Landscape");
    [mainWebView stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@"document.getElementById(\"view\").setAttribute('content', 'width=1014');"]];
}
return YES;}

- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation {
NSLog(@"didRotateFromInterfaceOrientation");
if(fromInterfaceOrientation == UIDeviceOrientationLandscapeLeft || fromInterfaceOrientation == UIDeviceOrientationLandscapeRight) {
    [mainWebView stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@"document.getElementById(\"view\").setAttribute('content', 'width=1014');"]];
    [mainWebView setAutoresizingMask:(UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth)];
    mainWebView.scalesPageToFit = YES;
} else if (fromInterfaceOrientation == UIDeviceOrientationPortrait || fromInterfaceOrientation == UIDeviceOrientationPortraitUpsideDown) {
    [mainWebView stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@"document.getElementById(\"view\").setAttribute('content', 'width=760');"]];
    [mainWebView setAutoresizingMask:(UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth)];
    mainWebView.scalesPageToFit = YES;
}}

PS shouldAutorotateToInterfaceOrientation 甚至没有被调用。

请告诉我我的代码有什么问题。任何建议都会有所帮助

4

0 回答 0