2012-01-08 13 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

この回答の可能な複製:[目的地Cのdidloadメソッドの開始時のLandscapeOrientation](http://stackoverflow.com/questions/8775911/landscapeorientation-on-start-of-didload-method-in-objective-c示唆されるように) – Pfitz

+0

回転にサブビューを整列させることにより問題を修正[ページングのクリーン自転遷移UIScrollViewの] [1] [1]:http://stackoverflow.com/questions/3322554/clean-オートローテーション - ページング - ウイスコンシブビュー – Eric

+0

デバイスを回転させるときにスクロールビューの寸法を変更する必要があります – Dhara

答えて

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]; 
    } 

あなたはこのコードを試すことができます。使用される値は、要件に基づいて調整可能です。ローテーションを使用してフレームを自分自身で設定しようとすると、自動サイズ調整マスクを使用しないでください。そうしないと、すべてが間違ってしまう可能性があります。あなたの質問に答えることを願っています。

関連する問題