【问题标题】:UIWebView pich to zoom work on iPhone 5.0 simulator but not work in iPhone 4.3 and iPad 4.3, 5.0 simulator?UIWebView pich 可以在 iPhone 5.0 模拟器上进行缩放,但不能在 iPhone 4.3 和 iPad 4.3、5.0 模拟器上工作?
【发布时间】:2012-08-10 14:16:24
【问题描述】:

我写了一个自定义的 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 个函数来放大、缩小和加载 HTMLString 到下面的 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 按钮时 --> webView zoomIn 通过调用 zoomIn 函数(与 zoomOut 相同)

当我在 iPhone 5.0 模拟器上测试时一切正常(我使用 xCode 4.2)。您可以捏合以放大、缩小(也可以使用 zoomIn、zoomOut 按钮)。 但是在 Iphone 4.3 和 iPad(4.3 和 5.0)上测试时 ---> 没有发生任何事情 --> LOG 显示:

**设置 ZoomScale 工作

滚动视图的缩放比例= 1.00; ---> 我也设置了另一个值,但 zoomScale 始终 = 1.00 --> 没有变化**

我的代码有什么问题??我很困惑。请帮助我。 提前致谢。

【问题讨论】:

    标签: ios xcode ipad uiwebview zooming


    【解决方案1】:

    这是用于滚动视图缩放方法..

    -(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;
    }
    

    如果不工作请告知...

    【讨论】:

      【解决方案2】:

      最好总是使用 self."iVar name"。

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

      然后像 self.webView 一样引用它。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2012-08-23
        • 2012-03-09
        • 1970-01-01
        • 1970-01-01
        • 2012-10-25
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多