Objective-C UIScrollView约束
我有一个Objective-C UIScrollView约束,objective-c,uiscrollview,constraints,autolayout,Objective C,Uiscrollview,Constraints,Autolayout,我有一个UIScrollview,它将包含X个视图,每个视图都是“CategoryPostView”类类型,其中包含一个UIImageView。我在滚动视图上一个接一个地添加这些视图。但是当图像加载到图像视图中时,我想更改图像视图的大小,因此我想更改滚动视图中所有其他视图的偏移量y。 我正在UIScrollView上使用autolayout约束,但无法获得任何结果。这是我正在使用的代码: int categoryScrollY = 0.0; CategoryPostVie
UIScrollview
,它将包含X个视图,每个视图都是“CategoryPostView”类类型,其中包含一个UIImageView
。我在滚动视图上一个接一个地添加这些视图。但是当图像加载到图像视图中时,我想更改图像视图的大小,因此我想更改滚动视图中所有其他视图的偏移量y。
我正在UIScrollView
上使用autolayout
约束,但无法获得任何结果。这是我正在使用的代码:
int categoryScrollY = 0.0;
CategoryPostView *previousView = nil;
for(; self.indexOfCate
goryInTheScroll < [self.categoryItemsArray count]; self.indexOfCategoryInTheScroll++){
PostItem *postItemObj = [self.categoryItemsArray objectAtIndex:self.indexOfCategoryInTheScroll];
CategoryPostView *categoryPostViewObj = [[CategoryPostView alloc] initWithFrame:CGRectMake(0.0, categoryScrollY, 148.0, 76.0)];
categoryPostViewObj.translatesAutoresizingMaskIntoConstraints = NO; //This part hung me up
[categoryPostViewObj setupPostViewWithItem:postItemObj];
[self.categoriesScrollView addSubview:categoryPostViewObj];
categoryScrollY += 80.0;
[_categoriesScrollView addConstraints:
[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[categoryPostViewObj]"
options:0 metrics:nil
views:@{@"categoryPostViewObj":categoryPostViewObj}]];
if (!previousView) { // first one, pin to top
[self.categoriesScrollView addConstraints:
[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[categoryPostViewObj]"
options:0 metrics:nil
views:@{@"categoryPostViewObj":categoryPostViewObj}]];
}else{
[self.categoriesScrollView addConstraints:
[NSLayoutConstraint
constraintsWithVisualFormat:@"V:[previousView][categoryPostViewObj]"
options:0 metrics:nil
views:@{@"categoryPostViewObj":categoryPostViewObj, @"previousView":previousView}]];
}
if(self.indexOfCategoryInTheScroll == ([self.categoryItemsArray count] - 1))
{
[self.categoriesScrollView addConstraint:[NSLayoutConstraint constraintWithItem:categoryPostViewObj
attribute:NSLayoutAttributeRight
relatedBy:NSLayoutRelationEqual
toItem:self.categoriesScrollView
attribute:NSLayoutAttributeRight
multiplier:1.0
constant:0]];
}
previousView = categoryPostViewObj;
}
非常感谢 在UIScrollView
内部使用约束的最佳方法是根本不使用它们
如果要使用约束布局滚动视图的内部,请创建UIView以包含滚动视图中的所有内容,然后将此视图放置到滚动视图中
通过这样做,您现在可以使用您创建的容器视图将所有约束放置在其中。我最终使用了UIView+AutoLayout类。
你可以在这里找到它
[self.categoriesScrollView addConstraints:
[NSLayoutConstraint
constraintsWithVisualFormat:@"V:[previousView][categoryPostViewObj]"
options:0 metrics:nil
views:@{@"categoryPostViewObj":categoryPostViewObj, @"previousView":previousView}]];