2012-08-10 201 views
1

我寫一個自定義視圖控制器具有的WebView象下面這樣:UIWebView pich縮放工作在iPhone 5.0模擬器上,但不能在iPhone 4.3和iPad 4.3,5.0模擬器中工作?

@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個功能zoomin,縮小和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按鈕用戶抽頭 - > webView的zoomIn由呼叫zoomIn函數(同樣爲縮小(ZoomOut))

一切正常,當我在iPhone 5.0模擬器測試(I xcode中使用4.2)。你可以捏縮放,zoomOut(也可以使用zoomIn,zoomOut按鈕)。 但是,當在iPhone 4.3和iPad測試(包括4.3和5.0)--->無事發生 - >的LOG表明:

* *設置ZoomScale工作

滾動型zoomScale = 1.00; --->我還設置其他值,但始終zoomScale = 1.00 - >無變化 **

什麼是錯我的代碼?我很困惑,請幫助我。 提前致謝。

回答

0

更好的是你總是使用自己的「iVar名字」。

​​

和後來的引用像self.webView。

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

如果沒有工作讓知道...

相關問題