在横向模式下将子视图添加到UIScrollView [英] Adding subviews to UIScrollView in landscape mode

查看:106
本文介绍了在横向模式下将子视图添加到UIScrollView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个视图控制器,它以横向模式启动,里面有UIScrollView。我尝试创建子视图并将它们添加到UIScrollView,但视图的帧大小都是纵向大小。

I have a view controller that starts in landscape mode with a UIScrollView in it. I try to create subviews and add them to the UIScrollView but the frame size of the views were all in portrait size.

这是我的代码:

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

输出:

推荐答案

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

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

You can try this code. The values used are tweakable based on your requirement. If you try to set the frame on your own with rotations make sure that you dont use the autoresizing mask or else everything can go wrong. Hope that answers to your question.

这篇关于在横向模式下将子视图添加到UIScrollView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆