![](/img/trans.png)
[英]UIStoryBoard Auto Layout embed UIStackView inside UIScrollView
[英]UIImageViews embed in UIScrollView in code w/ auto layout on
我目前在使用iOS應用和自動版式時遇到問題。 我想讓用戶在UIScrollView
滾動Tumblr中的照片(滾動圖庫時類似於“照片”應用程序)。 因此,當我的ImageViewController出現在屏幕上時,它具有一組帖子,但是還沒有圖像的數據。 我的問題如下:
在我的scrollView中,我想根據需要添加任意數量的UIImageViewS,並且希望它們的大小都相同。 我該怎么辦? 我嘗試了許多(可能是設計不好的:-/)方法,例如initwithframe:
了自動布局,並在NSMutableArray
保留了對它們的引用。
我現在的目標是將它們添加到viewDidLoad
scrollView中,並設置正確的約束。
感謝您的幫助,對不起我的英語不好
編輯
好的,我解決了我的問題:我使用了一個不錯的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];
}
-(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;
}
}
希望對您有幫助。
我建議不要使用scrollView,而應使用collectionView(iOS 6及更高版本)。 您可能會堅持認為需要使用iOS 5或iOS4。使自己的事情變得容易,collectionViews使內存管理變得容易,並且如果您打算在視圖中加載很多照片,只需使用集合視圖即可。 然后,您可以創建一個具有UIImageView的collectionViewCell。 UIImageView將只有一種尺寸,然后可以設置其圖像縮放比例屬性以進行縮放以適合任何形狀或大小的圖像,這些圖像將適合圖像視圖且不會變形。 對Collection視圖進行一些研究,哎呀,可能有一個教程可以用來從Internet加載圖像並使用集合視圖顯示它們。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.