В настоящее время у меня проблемы с моим приложением iOS и Autolayout. Я хочу, чтобы пользователь прокручивал фотографии из Tumblr в UIScrollView
(аналогично приложению «Фото» при прокрутке вашей библиотеки). Поэтому, когда мой ImageViewController появляется на экране, у него есть массив сообщений, но еще нет данных для изображений. Моя проблема заключается в следующем:
В моем scrollView я хочу добавить столько UIImageViewS, сколько необходимо, и я хотел бы, чтобы все они были одинакового размера. Как я должен это делать? Я пробовал много (вероятно, плохо разработанных :-/) способов, таких как initwithframe:
с включенной автоматической компоновкой и сохранением ссылки на них в NSMutableArray
...
Теперь моя цель - добавить их в мой scrollView в viewDidLoad
и установить правильные ограничения.
Спасибо за вашу помощь и извините за мой плохой английский
ИЗМЕНИТЬ
ОК, я решил свою проблему: я использовал хороший scrollView и установил его ограничения с автоматической компоновкой в viewWillAppear
Вот код для интересующихся (извините за макет): - (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];
}