UIWebView pich可以在iPhone 5.0模拟器上进行缩放工作,但在iPhone 4.3和iPad 4.3,5.0模拟器中无法正常工作?

时间:2012-08-10 14:16:24

标签: ios xcode ipad uiwebview zooming

我编写的自定义ViewController有一个如下所示的webview:

@interface WebpageController : UIViewController<UIWebViewDelegate> {
    UIWebView* webView;
//....
}

和构造函数:

-(id)init {

    if (self = [super init]) {
    webView=[[UIWebView alloc]init];
    webView.delegate=self;
        webView.scalesPageToFit = YES;
        webView.multipleTouchEnabled = YES;
//.....
}

我有3个函数来缩放,缩小和loadHTMLString到webView下面:

//this is load HTML String function
// param: content --> HTML conten by inside <body> </body>
 -(void) loadWebViewWithContent:(NSString*) content{

    NSString * header = @"<meta name='viewport' content='width=device-width;initial-scale=1.0; user-scalable= yes;'/>";

    NSString * html = [NSString stringWithFormat:@"<html> 
                                                      <head>%@ </head> 
                                                      <body> %@ </body>
                                                   </html>",header,content];
    [webView loadHTMLString:html baseURL:nil];

}

//这些是放大和缩小功能

-(void) zoomIn {
    for (UIScrollView *scroll in [self.webView subviews]) {
        //Set the zoom level.
        if ([scroll respondsToSelector:@selector(setZoomScale:animated:)]) {
            NSLog(@"set ZoomScale work");

            [scroll setZoomScale:2.0f animated:YES];
            NSLog(@"zoomScale of SCROLLVIEW= %f",(scroll.zoomScale));

        }
    }

}
-(void) zoomOut {

    for (UIScrollView *scroll in [self.webView subviews]) {
        //Set the zoom level.

        if ([scroll respondsToSelector:@selector(setZoomScale:animated:)]) {

            NSLog(@"set ZoomScale work");
            [scroll setZoomScale:0.5f animated:YES];
            NSLog(@"zoomScale of SCROLLVIEW= %f",(scroll.zoomScale));

        }

    }

}

在主UI中,我设置了当用户点击ZoomIn按钮 - &gt; webView zoomIn通过调用zoomIn函数(对于zoomOut也一样)

我在iPhone 5.0模拟器上测试时一切正常(我使用xCode 4.2)。你可以捏到zoomin,zoomOut(也可以使用zoomIn,zoomOut按钮)。 但是当测试Iphone 4.3和IPad(4.3和5.0)---&gt;没有事情发生 - &gt; LOG显示:

* *设置ZoomScale工作

SCROLLVIEW的缩放比例= 1.00;  的 ---&GT;我也设置了另一个值,但zoomScale始终= 1.00 - &gt;没有变化 **

我的代码出了什么问题?我很困惑。请帮帮我。 提前谢谢。

2 个答案:

答案 0 :(得分:1)

这适用于scrollview缩放方法..

-(UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView 
{
    return contentView;
}

-(void)scrollViewDidZoom:(UIScrollView *)scrllView 
{
    CGRect rectZoom = [self centeredFrameForScrollView:scrllView andUIView:contentView]; 
    contentView.frame = rectZoom; //[self centeredFrameForScrollView:scrllView andUIView:contentView];
}

-(CGRect)centeredFrameForScrollView:(UIScrollView *)scroll andUIView:(UIView *)rView 
{
    CGSize boundsSize = scroll.bounds.size;
    CGRect frameToCenter = rView.frame;

    // center horizontally
    if (frameToCenter.size.width < boundsSize.width) 
    {
        frameToCenter.origin.x = (boundsSize.width - frameToCenter.size.width) / 2;
    }
    else 
    {
        frameToCenter.origin.x = 0;
    }

    // center vertically
    if (frameToCenter.size.height < boundsSize.height) 
    {
        frameToCenter.origin.y = (boundsSize.height - frameToCenter.size.height) / 2;
    }
    else 
    {
        frameToCenter.origin.y = 0;
    }
    frameToCenter = CGRectMake(frameToCenter.origin.x, frameToCenter.origin.y, frameToCenter.size.width, frameToCenter.size.height);
    return frameToCenter;
}

如果不行,就让我们知道......

答案 1 :(得分:0)

最好总是使用自我。“iVar name”。

@interface WebpageController : UIViewController<UIWebViewDelegate>
@property (strong, nonatomic) UIWebView *webview;
@end
@implementation
  @synthesize webView;
@end

稍后将其称为self.webView。