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忽略捏和缩放?
谢谢!
所以我现在遇到的问题是UIWebViews显示单个图像.我想要的是如果图像不适合该地方则要缩小图像,如果没有则保持原始尺寸.所以这是我如何做到的(在UIViewController中):
- (void)viewDidLoad {
[super viewDidLoad];
UIWebView *webView = [[UIWebView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 80)];
NSString *path =[[NSBundle mainBundle] bundlePath];
NSURL *baseUrl =[NSURL fileURLWithPath:path];
NSString *html = [NSString stringWithFormat:@"<html><head>"
//THE LINE ABOVE IS TO PREVENT THE VIEW TO BE SCROLLABLE
"<script>document.ontouchstart = function(event) { event.preventDefault(); }</script>"
"</head><body style=\"text-align:center;margin:0;padding:0\">"
"<img src=\"banner.gif\" />"
"</body>"
"</html>"];
webView.scalesPageToFit = YES;
[webView loadHTMLString:html baseURL:baseUrl];
[self.view addSubview:webView];
[webView release];
}
Run Code Online (Sandbox Code Playgroud)
这会裁剪图像并使webview可滚动,这不是所需的行为.
所以我开始尝试在图像标记中使用一些CSS:
"<img style=\"width:100%\" src=\"banner.gif\" />"
Run Code Online (Sandbox Code Playgroud)
要么
"<img style=\"max-width:100%\" src=\"banner.gif\" />"
Run Code Online (Sandbox Code Playgroud)
我不知道为什么,但100%似乎不是UIView的宽度,它真的很小!(视图中的源图像大于320px,这是结果:)

所以我试着设置身体的宽度:
"</head><body style=\"width:100%;text-align:center;margin:0;padding:0\">"
"<img style=\"width:100%\" …Run Code Online (Sandbox Code Playgroud)