【问题标题】:UIScrollview containing many imagesUIScrollview 包含许多图像
【发布时间】:2013-04-19 23:26:21
【问题描述】:

我在 Xcode 4.6 中使用 UIScrollview。我想在滚动视图中插入大约 30 张图像。在情节提要中,我不能添加这么多,因为它实际上并不能让您在情节提要上向下滚动以添加更多图像,因此所有图像都相互重叠。

答案可能很简单,如果这是一个愚蠢的问题,请见谅。如何将这么多图像添加到滚动视图中,有没有办法像在 Android xml 中一样对其进行编码?还是有没有图像重叠?

【问题讨论】:

  • 请注意滚动视图的内容大小。确保内容大小等于或大于所有图像高度的总和..

标签: iphone objective-c xcode


【解决方案1】:

还可以考虑使用 UITableView。这可能是处理这么多图像的更有效方式。您可以在它们显示时加载它们,而不是一次全部加载。

【讨论】:

    【解决方案2】:

    您不会希望在 Interface Builder 中执行此操作 - 这将使其成为创建和维护的噩梦。

    相反,在代码中进行。您需要通过 Google UIScrollView tutorial 开始使用。这真的很简单 - 比手动拖出一堆图像视图要容易得多。

    【讨论】:

      【解决方案3】:

      所以这是我以前解决过的问题。有几种方法可以解决这个问题,我认为至少在 iOS 6.x 中新的首选方法可以选择使用 UICollectionView。

      UICollectionView View Apple Docs

      这是一个很棒的教程网站,包含大量有用的信息

      UICollectionView Tutorial

      我想出的一种解决方案是手动加载和放置图像。然后根据宽度所需的大小设置我的 UIScrollView setcontentSize 属性。

      //Clean Up and remove old buttons
      for(int i=0; i < _localButtonArray.count; i++){
          [(CategoryButton*)[_localButtonArray objectAtIndex:i] removeFromSuperview];
      }
      [_localButtonArray removeAllObjects];
      
      CGFloat baseX = 10,
              X = 0,
              Y = 0;
      int xPadding = 20,
          yPadding = 12,
          index = 0,
          maxRow = 4,
          maxCol = 4,
          colCount = 0;
      CGRect buttonFrame;
      
      buttonFrame.size.height = 137;
      buttonFrame.size.width  = 137;
      buttonFrame.origin.y    = X;
      buttonFrame.origin.x    = Y;
      
      
      
      for(int i = 0;i < _localInfoArray.count ; i++){
      
          id object = [_localInfoArray objectAtIndex:i];
      
          if(index >= maxRow){
              index = 0;
              X = baseX;
              Y += buttonFrame.size.height + yPadding;
              if(colCount >= (maxRow * maxCol)){
                  colCount=0;
                  baseX += 637;
                  Y = 0;
              }
          }
          X = baseX + (index * (buttonFrame.size.width + xPadding));
          index++;
          colCount++;
      
          buttonFrame.origin.x = X;
          buttonFrame.origin.y = Y + yPadding;
          /*
           * Custom button class
           */
          CategoryButton * categoryButton = [[CategoryButton alloc]initWithFrame:buttonFrame Object:object];
      
      
      
          //Add action
          [categoryButton addTarget:self action:@selector(categoryButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
      
          [categoryButton.titleLabel setNumberOfLines:3];
          [categoryButton.titleLabel setLineBreakMode:NSLineBreakByWordWrapping];
          [categoryButton.titleLabel setFont:[UIFont fontWithName:@"Helvetica-Bold" size:19]];
          [categoryButton.titleLabel setTextAlignment:NSTextAlignmentCenter];
          [categoryButton setMultipleTouchEnabled:NO];
      
          [_localButtonArray addObject:categoryButton];
          [self.view addSubview:categoryButton];
      
      
      
      }
      

      还有很多其他代码不在此处,但页面布局在此处处理。 即使这不是最佳解决方案,但希望它会对您有所帮助。

      祝你好运^_^

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多