2

我有一个已添加到 xib 的 webview,它处于纵向模式。我已将 xib 中的 webview 框架设置为 (0,54,768,905)。当我在纵向模式下运行我的应用程序时,它会在正确的框架中显示 webview,但是当我从纵向旋转到横向时,我的 webview 框架没有正确设置。

在将旋转界面方向方法中,我设置了 webview 的框架,但没有反映任何变化。当我从纵向旋转到横向时,webview 的框架从 y 位置 = 0 而不是 54 开始。

这是我的代码:

  -(void)viewDidLoad
{  

        //Landscape
        viewstatus =@"landscape";


        if (webview)
        {
            [webview removeFromSuperview];
        }
        self.webview.frame =CGRectMake(0, self.view.origin.y+54, 1024, 680);
        [self.view addSubview:webview];
        NSURL *url = [NSURL fileURLWithPath:dataObject];
        NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
        [webview loadRequest:requestObj];
        NSString *insertRule1 = [NSString stringWithFormat:@"addCSSRule('html', 'padding: 0px; height: %fpx; -webkit-column-gap: 0px; -webkit-column-width: %fpx;')", 680.0f, 1024.0f];
        [webview stringByEvaluatingJavaScriptFromString:insertRule1];

        pagecontrol.frame =CGRectMake(300, 710, 150, 36);

    }else{

        //Portrait


        if (webview)
        {
            [webview removeFromSuperview];
        }
        self.webview.frame =CGRectMake(0, self.view.origin.y+54, 768, 905);
        [self.view addSubview:webview];
        NSURL *url = [NSURL fileURLWithPath:dataObject];
        NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
        [webview loadRequest:requestObj];


        NSString *insertRule1 = [NSString stringWithFormat:@"addCSSRule('html', 'padding: 0px; height: %fpx; -webkit-column-gap: 0px; -webkit-column-width: %fpx;')", 905.0f, 768.0f];
        [webview stringByEvaluatingJavaScriptFromString:insertRule1];
        pagecontrol.frame =CGRectMake(208, 959, 358, 36);

    }

    UITapGestureRecognizer* doubleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action: @selector(handleDoubleTap:)];
    doubleTap.numberOfTouchesRequired = 2 ;
    [self.view addGestureRecognizer:doubleTap] ;
    UIScrollView* sv = nil;
    for(UIView* v in self.webview.subviews){
        if([v isKindOfClass:[UIScrollView class] ]){
            sv = (UIScrollView*) v;
            [sv setZoomScale:9.0f animated:YES];
            //sv.scrollEnabled=YES;
            sv.bounces = NO;
        }
    }


    [pagecontrol setAutoresizingMask:UIViewAutoresizingFlexibleWidth];
    [pagecontrol addTarget:self action:@selector(changePage:) forControlEvents:UIControlEventValueChanged];
    [self.view addSubview:pagecontrol];
    webview.scrollView.delegate=self;
    webview.scrollView.showsVerticalScrollIndicator=NO;
    webview.scrollView.bounces = NO;
    webview.scrollView.pagingEnabled = YES;
    pinchdetect=NO;
    pinchRecognizer = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinchDetected:)];
    [self.webview addGestureRecognizer:pinchRecognizer];

   }    

}




- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{

    if(toInterfaceOrientation == 1 || toInterfaceOrientation == 2)
    {
        viewstatus =@"portrait";



        [self.view bringSubviewToFront:pagecontrol];
            if (webview)
        {
            [webview removeFromSuperview];
        }
        self.webview.frame =CGRectMake(0, self.view.origin.y+54, 768, 905);
        [self.view addSubview:webview];
        NSString *insertRule1 = [NSString stringWithFormat:@"addCSSRule('html', 'padding: 0px; height: %fpx; -webkit-column-gap: 0px; -webkit-column-width: %fpx;')", 905.0f, 768.0f];
        [webview stringByEvaluatingJavaScriptFromString:insertRule1];
           //     webview.frame =CGRectMake(0, 54, 768, 905);
        pagecontrol.frame =CGRectMake(230, 959, 600, 36);


    }
    if(toInterfaceOrientation == 3 || toInterfaceOrientation == 4)
    {
        viewstatus =@"landscape";
        if (webview)
        {
            [webview removeFromSuperview];
        }
        self.webview.frame =CGRectMake(0, self.view.origin.y+54, 1024, 680);
        [self.view addSubview:webview];
        [self.view bringSubviewToFront:pagecontrol];
        NSString *insertRule1 = [NSString stringWithFormat:@"addCSSRule('html', 'padding: 0px; height: %fpx; -webkit-column-gap: 0px; -webkit-column-width: %fpx;')", 680.0f, 1024.0f];
        [webview stringByEvaluatingJavaScriptFromString:insertRule1];

        pagecontrol.frame =CGRectMake(300, 710, 150, 36);


    }
    [webview reload];

}
4

2 回答 2

0

首先shouldAutorotateToInterfaceOrientationYES下面的方法设置

- (BOOL) shouldAutorotateToInterfaceOrientation: (UIInterfaceOrientation) orientation 
{
    return YES;
}

然后添加这个波纹管方法

- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
{
    if(fromInterfaceOrientation == UIInterfaceOrientationPortrait){
        [webView stringByEvaluatingJavaScriptFromString:@"rotate(0)"];
    }
    else{
        [webView stringByEvaluatingJavaScriptFromString:@"rotate(1)"];
    }
}
于 2013-05-22T12:49:43.207 回答
0

如果您使用自动布局和 IB,当您将 web 视图拖放到视图上时,请使用固定。将顶部、前导、尾随和底部固定到视图。无论您使用什么方向,它都会坚持下去。我这样做了几次,效果很好。

于 2013-05-22T15:12:09.223 回答