2012-01-08 61 views
3

我有一个视图控制器,在其中包含UIScrollView的横向模式下启动。我尝试创建子视图并将它们添加到UIScrollView,但视图的框架大小均为纵向大小。在横向模式下向UIScrollView添加子视图

这里是我的代码:

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    [self.scrollView setAutoresizingMask:(UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight)]; 

    NSArray *colors = [NSArray arrayWithObjects:[UIColor redColor], [UIColor greenColor], [UIColor blueColor], nil]; 

    for (int i = 0; i < colors.count; i++) 
    { 
     CGRect frame = self.scrollView.frame; 
     frame.origin.x = frame.size.width * i; 
     frame.origin.y = 0; 

     UIView *subview = [[UIView alloc] initWithFrame:frame]; 
     subview.backgroundColor = [colors objectAtIndex:i]; 
     [self.scrollView addSubview:subview]; 
    } 

    self.scrollView.contentSize = CGSizeMake(self.scrollView.frame.size.width * colors.count, 
              self.scrollView.frame.size.height); 
    self.scrollView.pagingEnabled = YES; 
    self.scrollView.showsHorizontalScrollIndicator = NO; 
    self.scrollView.showsVerticalScrollIndicator = NO; 
    self.scrollView.scrollsToTop = NO; 
} 

输出:

enter image description here

+0

可能的重复的这个答案:[LandscapeOrientation开始在目标c中的didload方法](http://stackoverflow.com/questions/8775911/landscapeorientation-on-start-of-didload-method-in-objective-c ) – Pfitz 2012-01-08 10:31:21

+0

通过对准上旋转的子视图修正问题如在建议[在寻呼清洁自转跃迁的UIScrollView] [1] [1]:http://stackoverflow.com/questions/3322554/clean-自动旋转 - 在分页-uiscrollview中转换 – Eric 2012-01-09 07:37:05

+0

当您旋转设备时,您需要更改滚动视图尺寸 – Dhara 2013-05-16 09:09:23

回答

0
- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    [self.scrollView setAutoresizingMask:(UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight)]; 
    NSArray *colors = [NSArray arrayWithObjects:[UIColor redColor], [UIColor greenColor], [UIColor blueColor], nil]; 
    for (int i = 0; i < colors.count; i++) 
    { 
     CGRect frame = self.scrollView.bounds; 
     frame.origin.x = frame.size.width * i; 
     frame.origin.y = 0; 
     UIView *subview = [[UIView alloc] initWithFrame:frame]; 
     subview.backgroundColor = [colors objectAtIndex:i]; 
     [self.scrollView addSubview:subview]; 
    } 
    [self.scrollView setAutoresizesSubviews:NO]; 
    self.scrollView.contentSize = CGSizeMake(self.scrollView.frame.size.width * colors.count,self.scrollView.frame.size.height); 
    self.scrollView.pagingEnabled = YES; 
    self.scrollView.showsHorizontalScrollIndicator = NO; 
    self.scrollView.showsVerticalScrollIndicator = NO; 
    self.scrollView.scrollsToTop = NO; 
} 

- (void) willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration 
    { 
    currentPageOffset = [self.scrollView contentOffset].x/[self.scrollView bounds].size.width; 
    if(UIInterfaceOrientationIsPortrait(toInterfaceOrientation)) 
    { 
     for(int i=0;i<=2;i++) 
     { 
      UIView *currentView = [[self.scrollView subviews] objectAtIndex:i]; 
      [currentView setFrame:CGRectMake((i)*currentView.frame.size.height, 0, [self.scrollView bounds].size.height,[self.scrollView bounds].size.width)]; 
    } 

    [self.scrollView setContentSize:CGSizeMake(self.scrollView.bounds.size.height *3, self.scrollView.bounds.size.width)]; 


    } 
    else 
    { 
      for(int i=0;i<=2;i++) 
      { 
       UIView *currentView = [[self.scrollView subviews] objectAtIndex:i]; 
       [currentView setFrame:CGRectMake((i)*currentView.frame.size.height, 0,[self.scrollView bounds].size.height,[self.scrollView bounds].size.width)]; 
      } 

      [self.scrollView setContentSize:CGSizeMake(self.scrollView.bounds.size.height * 3, self.scrollView.bounds.size.width)];   
    } 
    } 
    - (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation duration:(NSTimeInterval)duration 
    { 
     [self.scrollView setContentOffset:CGPointMake([self.scrollView bounds].size.width * currentPageOffset, 0.0f) animated:NO]; 
    } 

你可以试试这个代码。使用的值可根据您的要求进行调整。如果您尝试使用旋转设置框架,请确保不使用自动调整屏蔽,否则一切都会出错。希望能回答你的问题。

相关问题