Добавление подпредставлений в UIScrollView в ландшафтном режиме - PullRequest
3 голосов
/ 08 января 2012

У меня есть контроллер представления, который запускается в ландшафтном режиме с 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

1 Ответ

0 голосов
/ 16 мая 2013
- (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];
     }

Вы можете попробовать этот код. Используемые значения настраиваются в зависимости от ваших требований. Если вы пытаетесь установить кадр самостоятельно с поворотами, убедитесь, что вы не используете маску авторазмера, иначе все может пойти не так. Надеюсь, что ответ на ваш вопрос.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...