【问题标题】:UIScrollView Will not allow scrolling or interaction after rotatedUIScrollView 旋转后将不允许滚动或交互
【发布时间】:2013-01-11 21:00:10
【问题描述】:

在我的

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

方法,我有代码可以在应用程序随设备旋转时重新定位和调整滚动视图的大小。我使用以下代码:

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

    [super willRotateToInterfaceOrientation:toInterfaceOrientation duration:0.5f];

    if (toInterfaceOrientation == UIInterfaceOrientationLandscapeRight){
         //landscape
    [mT.buttonScroll setFrame:CGRectMake(0, 544, 1024, 160)];

    }else if (toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft){
        //landscape
    [mT.buttonScroll setFrame:CGRectMake(0, 544, 1024, 160)];

    }else if (toInterfaceOrientation == UIInterfaceOrientationPortrait){
       //portrait
    [mT.buttonScroll setFrame:CGRectMake(0, 800, 768, 160)];

    }else if (toInterfaceOrientation == UIInterfaceOrientationPortraitUpsideDown){
      //portrait
    [mT.buttonScroll setFrame:CGRectMake(0, 800, 768, 160)];

    }
}

一切都正常旋转,但是一旦我第二次旋转,滚动视图就变得完全不可触摸。无法滚动或触摸其中的任何按钮。然后,如果我旋转回上一个视图,触摸就会回来,依此类推。有谁知道它为什么这样做?

【问题讨论】:

  • 你更新滚动视图的内容大小了吗?

标签: ios uiscrollview ios-simulator uiinterfaceorientation uitouch


【解决方案1】:

更改Frame 一切都很好,但是当涉及到UIScrollViews 时,contentSize 就完全不同了。更改方向时需要设置contentSize。您可以通过在更改方向时添加类似这样的内容来做到这一点。

[mT.buttonScroll setContentSize:CGSizeMake(width, height)];

我假设mT.buttonScroll 是某种UIScrollView

这里发生的是您将内容区域的大小重置为您想要的大小。您可以在此处修改宽度和高度。

如果不给我们评论,希望这会有所帮助,我会尽力提供帮助。

【讨论】:

    【解决方案2】:

    我认为您应该使用 shouldAutorotateToInterfaceOrientation 并观察您的旋转和 UI 响应。

    在我的测试中,我提到的方法可以正常工作。 willRotatetoInterfaceOrientation 在其“应该”对应物之后被调用。否则,它根本不会被调用。

    【讨论】:

      【解决方案3】:

      除了大力水手的回答(这是正确的;)我发现我需要像这样在整个地方设置内容大小以避免这个问题。

      -(void)viewDidAppear:(BOOL)animated{
      [super viewDidAppear:animated];
      [scroller setScrollEnabled:YES];
      [scroller setContentSize:CGSizeMake (0,100)];
      }
      
      -(void)viewWillAppear:(BOOL)animated{
      [super viewWillAppear:animated];
      [scroller setScrollEnabled:YES];
      [scroller setContentSize:CGSizeMake (0,100) ];
      }
      
      -(void)viewDidLoad
      {
      [super viewDidLoad];
      [scroller setScrollEnabled:YES];
      [scroller setContentSize:CGSizeMake (0,100) ];
      }
      
      -(void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
      {
      if ([[UIDevice currentDevice] orientation] == UIDeviceOrientationLandscapeLeft || [[UIDevice currentDevice] orientation] == UIDeviceOrientationLandscapeRight)
      {
      NSLog(@"Lanscapse");
      [scroller setScrollEnabled:YES];
      [scroller setContentSize:CGSizeMake (0,100];
      }
      if([[UIDevice currentDevice] orientation] == UIDeviceOrientationPortrait || [[UIDevice currentDevice] orientation] == UIDeviceOrientationPortraitUpsideDown )
      {
      NSLog(@"Portrait");
      [scroller setScrollEnabled:YES];
      [scroller setContentSize:CGSizeMake (0,100) ];
      }
      }
      

      【讨论】:

        【解决方案4】:

        我也面临同样的问题 请按照以下步骤操作: 1.给滚动视图的顶部、底部、前导和底部约束 2.滚动视图中的包装视图给出顶部、底部、前导和底部以及与父视图相等的宽度。 3. 非常重要的一步:将所有内部UI元素与前导、尾随、顶部、底部和高度相关联。特别是最后一个元素记得添加底部以及高度和尾随

        【讨论】:

          【解决方案5】:

          检查滚动视图的框架。旋转后滚动视图的框架穿过窗口框架,因此滚动视图没有响应触摸。

          我还假设在计算 rect 时,您会考虑其他视图 rect(如果存在),例如导航栏、标签栏、状态栏

          【讨论】:

            猜你喜欢
            • 2011-07-01
            • 2012-04-16
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2012-07-15
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多