1

我试图在 webview 中加载一些 html,我希望将不同的 css 用于横向和纵向

这是我的html的头部

NSMutableString *htmlString = [NSMutableString stringWithString:@"<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">"];

        [htmlString appendString:@"<html xmlns=\"http://www.w3.org/1999/xhtml\" xml:lang=\"en\" id=\"bp-doc\">"
         "<head>"
         "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\"/>"
         "<meta name=\"viewport\" content=\"width=device-width; initial-scale=1.0; maximum-scale=1.0; user-scalable=0;\" />" ];
        //landscape css
        [htmlString appendString:@"<link rel='stylesheet' media='screen and (orientation: landscape)' href='landscape.css' type='text/css'/>"];
        //portrait css
        [htmlString appendString:@"<link rel='stylesheet' media='screen and (orientation: portrait)' href='portrait.css' type='text/css'/>"];

        [htmlString appendString:@"<link rel='stylesheet' href='style.css' type='text/css'/>"
         "<script type='text/javascript' src='jquery-1.6.2.min.js'></script>"
          "<script type='text/javascript' src='script.js'></script>"
         "</head>"
         ];

我也在视图控制器中执行此操作 shouldAutorotateToInterfaceOrientation: 方法

int i =0;
    switch (interfaceOrientation){
        case UIInterfaceOrientationPortrait:
            i = 0;
            break;
        case UIInterfaceOrientationPortraitUpsideDown:
            i = 180;
            break;
        case UIInterfaceOrientationLandscapeLeft:
            i = 90;
            break;
        case UIInterfaceOrientationLandscapeRight:
            i = -90;
            break;
    }
    [webView stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@"window.__defineGetter__('orientation',function(){return %d;});window.onorientationchange();",i]];

但是portrait.css 从不用于纵向。似乎方向肖像的媒体查询根本不起作用。

4

0 回答 0