objective-c - 在横向模式下向 UIScrollView 添加 subview

标签 objective-c ios uiview uiscrollview

我有一个以横向模式启动的 View Controller ,其中有一个 UIScrollView。我尝试创建 subview 并将它们添加到 UIScrollView,但 View 的框架大小都是纵向大小。

这是我的代码:

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

最佳答案

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

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

关于objective-c - 在横向模式下向 UIScrollView 添加 subview ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8776040/

相关文章:

ios - 从 iOS 10.3 开始 : UIButton ignoring negative paragraph lineSpacing in NSAttributedString

objective-c - 将 float 格式化为 objective-c 中小数点后两位或更少的字符串

ios - iPhone-播放电影后,播放音频效果不佳

swift - 为什么我的背景渐变 View 没有加载到我的客户 UINavigationController 类中

ios - 模拟一个 "picture taken"的屏幕闪

ios - 在 Swift 的 UIView 中查找 UILabel

iphone - 用户退出时停止,无效并设置为NSTimer为Nil

objective-c - "Creating selector for nonexistent method ' 的 Xcode 虚假警告比较 :'"

ios - 如何添加 Rate App 来解锁某些东西

iphone - 以编程方式列出 iPhone 上所有已安装的应用程序?