如何在iOS中设置方向变化的webview框架?

Posted

技术标签:

【中文标题】如何在iOS中设置方向变化的webview框架?【英文标题】:How to set the frame of webview on orientation change in iOS? 【发布时间】:2013-05-22 12:40:40 【问题描述】:

我有一个已添加到 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:@"addCs-s-rule('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:@"addCs-s-rule('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:@"addCs-s-rule('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:@"addCs-s-rule('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];


【问题讨论】:

为什么不尝试将 AutoresizingMask 设置为 webview ..[webView setAutoresizingMask:UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleWidth | [UIViewAutoresizingFlexibleHeight ]; 【参考方案1】:

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

【讨论】:

【参考方案2】:

首先使用以下方法将shouldAutorotateToInterfaceOrientation 设置为YES

- (BOOL) shouldAutorotateToInterfaceOrientation: (UIInterfaceOrientation) orientation 

    return YES;

然后添加下面的方法

- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation

    if(fromInterfaceOrientation == UIInterfaceOrientationPortrait)
        [webView stringByEvaluatingJavaScriptFromString:@"rotate(0)"];
    
    else
        [webView stringByEvaluatingJavaScriptFromString:@"rotate(1)"];
    

【讨论】:

以上是关于如何在iOS中设置方向变化的webview框架?的主要内容,如果未能解决你的问题,请参考以下文章

如何在 iOS 的 XMPP 框架中设置资源

iOS:如何获取刚刚在界面生成器中设置的 UIView 的框架

在 iOS 7 中设置方向

iphone:如何在 webView 中设置字体大小?

在 UINavigationBar 的子类中设置 backButton 框架

如何在 WebView2 中设置基于选项卡的 cookie?