2

I am currently having trouble with my iOS app and Autolayout. I want to let the user scroll photos from Tumblr in a UIScrollView (similar to Photos app, when scrolling your library). So when my ImageViewController gets on screen, it has an array of posts but no data for the images yet. My problem is the following :

In my scrollView, I want to add as many UIImageViewS as needed, and I would like them to be all the same size. How should I do it? I tried many (probably bad designed :-/) ways, like initwithframe: with auto layout on and keeping a reference to them in a NSMutableArray...

My goal now is to add them to my scrollView in viewDidLoad and have correct constraints set.

Thanks for your help and sorry for my poor English

EDIT

OK I solved my problem: I used a nice scrollView and set its constraints with auto layout in viewWillAppear

Here is the code for those interested (sorry for layout) : - (void)viewWillAppear:(BOOL)animated {

[super viewWillAppear:animated];
[self downloadPhotos];
[self.scrollView setContentOffset:CGPointMake(self.selectedImageIndex * self.scrollView.frame.size.width, 0) animated:NO];

// Add UIImageViewS to self.scrollView with constraints and so on...
NSMutableDictionary *viewsDictionnary = [[NSMutableDictionary alloc] init];
NSMutableString *imageViewsString = [[NSMutableString alloc] init];
NSMutableArray *imageViews = [[NSMutableArray alloc] init];

for (int i = 0; i < self.fetchedResultsController.fetchedObjects.count; i++) {
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:self.scrollView.bounds];
    imageView.image = [UIImage imageNamed:@"placeholder_imageView"];
    imageView.contentMode = UIViewContentModeScaleAspectFit;
    imageView.translatesAutoresizingMaskIntoConstraints = NO;
    [self.scrollView addSubview:imageView];

    [imageViews addObject:imageView];
    [viewsDictionnary setObject:imageView forKey:[NSString stringWithFormat:@"imageView%d", i]];
    [imageViewsString appendString:[NSString stringWithFormat:@"[imageView%d]", i]];
    [self.view addConstraint:[NSLayoutConstraint constraintWithItem:imageView attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:self.scrollView attribute:NSLayoutAttributeCenterY multiplier:1.0 constant:0.0]];
    [self.view addConstraint:[NSLayoutConstraint constraintWithItem:imageView attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:self.scrollView attribute:NSLayoutAttributeWidth multiplier:1.0 constant:0.0]];
    [self.view addConstraint:[NSLayoutConstraint constraintWithItem:imageView attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:self.scrollView attribute:NSLayoutAttributeHeight multiplier:1.0 constant:0.0]];
}
self.imageViews = imageViews;
NSString *horizontal = [NSString stringWithFormat:@"H:|%@|", imageViewsString];
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:horizontal options:0 metrics:0 views:viewsDictionnary]];
[self.scrollView setContentOffset:CGPointMake(self.selectedImageIndex * self.scrollView.frame.size.width, 0) animated:NO];

}

4

2 回答 2

0

我建议不要使用scrollView,而是使用collectionView(iOS 6 及更高版本)。您可能会坚持认为您需要使用 iOS 5 或 iOS 4。让您自己轻松,collectionViews 使内存管理变得容易,如果您打算在视图中加载大量照片,只需使用 collection view。然后,您可以创建一个具有 UIImageView 的 collectionViewCell。UIImageView 将只有一种尺寸,然后您可以将其图像缩放属性设置为缩放以适应任何形状或大小,图像将适合图像视图并且不会失真。对集合视图进行一些研究,哎呀,可能有一个教程可以用来从互联网加载图像并使用集合视图显示它们。

于 2013-05-05T18:07:25.563 回答
0
 -(void)viewDidLoad 
 {
      [super viewDidLoad];

      float posX = 0.0 , poxY = 0.0;

      float sizeWidth = 100 , sizeHeight = 200;

      float scrollContentWidth = 320.0;

      scrollview.showsHorizontalScrollIndicator=YES;
      scrollview.scrollEnabled=YES;
      scrollview.userInteractionEnabled=YES;
      self.scrollView.pagingEnabled = TRUE;

      for(int i=0;i<[imageArray count];i++)
      {
           NSString *imageURL = [imageArray objectAtIndex:i];

           UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(posX,posY,sizeWidth,sizeHeight)];

           [imageView setImage:[UIImage imageWithContentsOfFile:imageURL]];

           [self.scrollView addSubview:imageView];

           [imageView addConstraint:
           [NSLayoutConstraint constraintWithItem:imageView 
           attribute:NSLayoutAttributeWidth
           relatedBy:NSLayoutRelationEqual
           toItem:imageView 
           attribute:NSLayoutAttributeWidth
           multiplier:1
           constant:100]];

           [imageView  addConstraint:
           [NSLayoutConstraint constraintWithItem:imageView 
           attribute:NSLayoutAttributeHeight
           relatedBy:NSLayoutRelationEqual
           toItem:imageView 
           attribute:NSLayoutAttributeHeight
           multiplier:1
           constant:200]];

           [self.scrollView setContentSize:CGSizeMake(480.0,scrollContentWidth)];

           scrollContentWidth=scrollContentWidth+320.0;

           posX = posX + 320.0;
      }
 }

Hope it will help you.

于 2013-05-05T17:56:55.947 回答