【问题标题】:How to resize uiscrollview and its content on the change of orientation in iPhone?如何在 iPhone 中改变方向时调整 uiscrollview 及其内容的大小?
【发布时间】:2011-08-23 05:24:10
【问题描述】:

我正在开发一个项目,我在 UIscrollview 中添加了 UIWebviews 以显示描述。我这样做是因为我想添加滑动效果以移动到新的描述页面。现在,我想在方向改变(即纵向到横向或反之亦然)时调整 UIscrollview 及其内容(即 uiwebview)的大小。

请给我任何示例代码或任何建议。

【问题讨论】:

    标签: iphone uiwebview uiscrollview resize


    【解决方案1】:

    要调整 webview 的大小,您必须编写:-

    - (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
    {
        webview.scrollView.contentSize = CGSizeMake(scrollView.frame.size.width * someValue1, 
                                            scrollView.frame.size.height * someValue2);
    }
    

    【讨论】:

    • 正是我是这个领域的新手,但我想知道如果你不使用 void 那么它会将值返回给主@shweta
    【解决方案2】:

    您可以将您的代码放在以下代码块中:

    - (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
    {
    if ([self isPadPortrait]) {
        // Your code for Portrait
        // set frame here  
    } else if ([self isPadLandscape]) {
       // Your code for Landscape
       // set frame here 
    }
    

    以下代码将处理方向更改:

    - (BOOL)isPadPortrait
    {
    
        return (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad
                && (self.interfaceOrientation == UIInterfaceOrientationPortrait
                    || self.interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown));
    }
    
    - (BOOL)isPadLandscape
    {
    
        return (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad
                && (self.interfaceOrientation == UIInterfaceOrientationLandscapeRight
                    || self.interfaceOrientation == UIInterfaceOrientationLandscapeLeft));
    
    }
    

    【讨论】:

    • 谢谢。我能够检测到方向。但问题是调整 UIscrollview 及其内容的大小。请在那里帮助我。
    • 您可以在willAnimateRotationToInterfaceOrientation:中设置您的框架。你还需要什么?
    • 我只有滚动视图,我想更改其内容的大小。该怎么做?
    【解决方案3】:

    您可以设置scrollView的autoresizingMask,并根据您的要求将此代码添加到您的.m文件中。

    - (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
    {
        scrollView.contentSize = CGSizeMake(scrollView.frame.size.width * someValue1, 
                                            scrollView.frame.size.height * someValue2);
    }
    

    【讨论】:

    • 它会调整 uiwebview 的大小,即 uiscrollview 的内容
    • 如果您在 xib 文件中制作视图,则可以通过旋转视图来测试它,否则您需要制作转储 xib 文件以检查应用程序所需的 autoresizingMask
    【解决方案4】:

    这是一个类似问题及其解决方案的链接: Scale image to fit screen on iPhone rotation

    他们对 ScrollView 和 ImageView 使用了 AutoresizingMasks 和 ContentModes 的组合,尽管我认为相同的解决方案适用于您的 WebView。

    【讨论】:

      【解决方案5】:

      当方向改变时

      - (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation duration:(NSTimeInterval)duration
      
      {
      
      // this delegate method will called if we set should auto orientation return yes;
      when ever we changed orientation  so set frames.. 
      
      }
      

      例如

      - (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation duration:(NSTimeInterval)duration
      
      {
          appDelegate.interface=interfaceOrientation;
      
      
          if (interfaceOrientation == UIInterfaceOrientationLandscapeLeft || interfaceOrientation == UIInterfaceOrientationLandscapeRight)
      
      {
      
      //  SET FRAMES FOR LANDSCAPE HERE       
      
      
      }
      
          if (interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown || interfaceOrientation == UIInterfaceOrientationPortrait)
      
      { 
              //SET FRAMES FOR Portrait HERE              
      
      }
      
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-05-16
        • 2011-02-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多