将scalesPageToFit设置为YES后,UIWebview甚至不会缩放

Jon*_*nah 8 iphone zoom subview uiwebview

UIWebview当用户选择一个选项卡时,我有一个加载为子视图UISegmentedControl.出于某种原因,我不能让它允许pinch/zooming.我在viewDidLoad:方法中设置了以下代码,因此它应该可以工作.

self.myWebView = [[[UIWebView alloc] initWithFrame:self.view.frame] autorelease];
self.myWebView.backgroundColor = [UIColor whiteColor];
self.myWebView.scalesPageToFit = YES;
self.myWebView.autoresizingMask = (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight);
self.myWebView.delegate = self;
[self.view addSubview: myWebView];
Run Code Online (Sandbox Code Playgroud)

我尝试UIWebView从NIB 加载并以编程方式创建它无济于事.有什么我想念的吗?什么可能导致webview忽略捏和缩放?

谢谢!

Vai*_*ran 9

  [webView stringByEvaluatingJavaScriptFromString:@"document.body.style.zoom = 5.0;"];
Run Code Online (Sandbox Code Playgroud)

似乎是合适的解决方案


srg*_*szy 7

我通过设置视口解决了这个问题:

<meta name="viewport" content="width=device-width; initial-scale=1.0; maximum-scale=1.5; user-scalable=1"/>
Run Code Online (Sandbox Code Playgroud)

祝好运


Ste*_*ntz 6

我看到你正在设置autoresizingMask.这是否意味着您创建了UIWebView初始大小为CGRectZero?你能和文件互动吗?我的意思是,滚动/点击工作吗?


小智 5

根据我的经验,您需要在UIWebView加载之前设置scalesPageToFit.这意味着在viewDidLoad之前进行设置等.我所做的是在"shouldStartLoadWithRequest"中设置它

- (BOOL)webView:(UIWebView *)theWebView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
    //make sure that the page scales when it is loaded :-)
    theWebView.scalesPageToFit = YES;
    return YES;
}
Run Code Online (Sandbox Code Playgroud)

我对文档的解释是scalesPageToFit属性决定了如何加载页面.事后并没有改变事情.

希望这可以帮助.