简体   繁体   English

UIImageViews以自动布局的代码嵌入UIScrollView中

[英]UIImageViews embed in UIScrollView in code w/ auto layout on

I am currently having trouble with my iOS app and Autolayout. 我目前在使用iOS应用和自动版式时遇到问题。 I want to let the user scroll photos from Tumblr in a UIScrollView (similar to Photos app, when scrolling your library). 我想让用户在UIScrollView滚动Tumblr中的照片(滚动图库时类似于“照片”应用程序)。 So when my ImageViewController gets on screen, it has an array of posts but no data for the images yet. 因此,当我的ImageViewController出现在屏幕上时,它具有一组帖子,但是还没有图像的数据。 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. 在我的scrollView中,我想根据需要添加任意数量的UIImageViewS,并且希望它们的大小都相同。 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 ... 我尝试了许多(可能是设计不好的:-/)方法,例如initwithframe:了自动布局,并在NSMutableArray保留了对它们的引用。

My goal now is to add them to my scrollView in viewDidLoad and have correct constraints set. 我现在的目标是将它们添加到viewDidLoad scrollView中,并设置正确的约束。

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 好的,我解决了我的问题:我使用了一个不错的scrollView并在viewWillAppear使用自动布局设置了它的约束。

Here is the code for those interested (sorry for layout) : - (void)viewWillAppear:(BOOL)animated { 这是对那些感兴趣的代码(对不起布局):-(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];

} }

 -(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. 希望对您有帮助。

I recommend not using a scrollView but using a collectionView instead (iOS 6 and above). 我建议不要使用scrollView,而应使用collectionView(iOS 6及更高版本)。 You will probably insist that you need to use iOS 5 or iOS 4. Make things easy for yourself, collectionViews make memory management easy and if you plan on loading a lot of photos in view, just go with collection views. 您可能会坚持认为需要使用iOS 5或iOS4。使自己的事情变得容易,collectionViews使内存管理变得容易,并且如果您打算在视图中加载很多照片,只需使用集合视图即可。 You can then create a collectionViewCell which will have a UIImageView. 然后,您可以创建一个具有UIImageView的collectionViewCell。 The UIImageView will be only one size, you can then set its image scaling propery to scale to fit so no matter what shape or size, the images will fit in the image view and not be distorted. UIImageView将只有一种尺寸,然后可以设置其图像缩放比例属性以进行缩放以适合任何形状或大小的图像,这些图像将适合图像视图且不会变形。 Do some research on colletion views, heck, there probably is a tutorial you can use to load images from the internet and display them using a collection view. 对Collection视图进行一些研究,哎呀,可能有一个教程可以用来从Internet加载图像并使用集合视图显示它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM