代码之家  ›  专栏  ›  技术社区  ›  d2kagw

方向更改时uiscrollview未获得新大小

  •  7
  • d2kagw  · 技术社区  · 16 年前

    我有一个非常简单的 UIScrollView 只是不做它应该做的。 不确定这是API的错误还是我的代码中的错误。

    基本上,我有一个 UIViewController 用一个 UISCLVIEW 就像它的风景一样。 当我把它添加到 UIWindow 改变ipad的方向,我退出 UISCLVIEW S尺寸,不正确(?)报道。

    这是我的 ui视图控制器 实施:

    @implementation CustomViewController
    - (void)loadView {
      scrollView = [[[UIScrollView alloc] init] autorelease];
      scrollView.delegate = self;
      scrollView.backgroundColor = [UIColor redColor];
      self.view = scrollView;
    }
    
    - (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
      CGSize rect = scrollView.frame.size;
      NSLog(@"will rotate w%f h%f", rect.width, rect.height);
    }
    
    - (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
      CGSize rect = scrollView.frame.size;
      NSLog(@"will animate rotation w%f h%f", rect.width, rect.height);
    }
    
    - (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation {
      CGSize rect = scrollView.frame.size;
      NSLog(@"did rotate w%f h%f", rect.width, rect.height);
    }
    
    - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
      return YES;
    }
    
    @end
    

    当我运行上述代码时,我在控制台中看到以下条目:

    2010-07-11 11:03:05.214 Untitled2[6682:207] will rotate w768.000000 h1004.000000
    2010-07-11 11:03:05.214 Untitled2[6682:207] will animate rotation w748.000000 h1024.000000
    2010-07-11 11:03:05.619 Untitled2[6682:207] did rotate w748.000000 h1024.000000
    2010-07-11 11:03:07.951 Untitled2[6682:207] will rotate w748.000000 h1024.000000
    2010-07-11 11:03:07.958 Untitled2[6682:207] will animate rotation w768.000000 h1004.000000
    2010-07-11 11:03:08.367 Untitled2[6682:207] did rotate w768.000000 h1004.000000
    

    如您所见,方向更改会调整 UISCLVIEW ,但只允许新状态栏。 我希望宽度和高度会急剧变化,因为 UISCLVIEW 比它的高度还要宽,反之亦然。

    有没有办法 UISCLVIEW 报告它的实际尺寸?

    5 回复  |  直到 12 年前
        1
  •  4
  •   Preston Software    14 年前

    如果你还在找的话,我有答案。测试 界限 ,而不是框架。

    原因:帧是边界和应用于旋转的转换的派生属性。

        2
  •  2
  •   rickharrison    16 年前

    当超视图调整大小时,视图不会自动调整自身的大小,除非您特别将其设置为。对于该滚动视图,您需要告诉它在超视图时调整其宽度和高度。

    scrollView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
    
        3
  •  2
  •   Sufian    12 年前

    我注意到决定 contentSize 在里面 didRotateFromInterfaceOrientation 如果你从一个iphone nib文件开始,而不是一个专用的ipad nib文件,这也是不可能的:iphone nib文件中的帧要小得多,因此计算出的内容高度比你实际使用ipad时要大得多。苹果确实说过我必须创建一个单独的ipad nib文件…

    所以,我也得到了旧的尺寸 双旋转共面方向 但对我来说,现在问题已经小了,因为一旦我开始使用正确的NIB文件,iOS就可以处理所有的问题。不过,在我看来,在 双旋转共面方向 方法当前视图的框架尺寸不正确。

    我最终找到了一个解决方案,只是使用通知而不是 双旋转共面方向 .

    [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
            [[NSNotificationCenter defaultCenter] addObserver:self
                                                     selector:@selector(orientationChanged:)
                                                     name:UIDeviceOrientationDidChangeNotification
                                                   object:nil];
    

    然后在 orientationChanged 方法,将视图调整为新框架。最后一个问题是:如果初始方向与NIB中的方向不同,那么视图仍然是错误的,我通过创建一个新的NIB文件来解决这个问题,特别是对于景观视图。在init方法中, ViewController 根据当前方向选择加载的笔尖。

        4
  •  0
  •   Hiren anka    14 年前

    我不知道这对你有帮助,因为我从来没有测试过这个,但我认为它可能对你有帮助

    - (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
    self.view = scrollView;
    CGSize rect = scrollView.frame.size;
    NSLog(@"will rotate w%f h%f", rect.width, rect.height);
    }
    
    - (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
    self.view = scrollView;    
    CGSize rect = scrollView.frame.size;
    NSLog(@"will animate rotation w%f h%f", rect.width, rect.height);
    }
    
    - (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation {
     self.view = scrollView;
     CGSize rect = scrollView.frame.size;
     NSLog(@"did rotate w%f h%f", rect.width, rect.height);
    }
    
    - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    return YES;
     }  
    
        5
  •  0
  •   Sufian    12 年前

    我通常通过设置 contentSize 到0样 CGSizeMake(contentSize.width, 0) 用于水平视图。

    逼迫 UIScrollView 仅滚动,例如水平设置不可滚动值 内容大小 至0 gt; cgsizemake(contentsize.width,0) .

    希望能有所帮助。

    推荐文章