gpt4 book ai didi

Objective-C UIScrollView 约束

转载 作者:搜寻专家 更新时间:2023-10-30 20:09:05 24 4
gpt4 key购买 nike

我有一个包含 X 个 View 的 UIScrollview,每个 View 都是包含一个 UIImageView 的类类型“CategoryPostView”。我在 scrollView 上一个接一个地添加这些 View 。但是当图像加载到 imageView 中时,我想更改 imageView 的大小,因此我想更改 中所有其他 View 的偏移量 y ScrollView 。我在 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;

}

目前,我的观点是相互叠加的。我必须以某种方式从前一个 View 的底部而不是从前一个 View 的顶部应用我的约束。我想我应该为这行代码添加选项:

[self.categoriesScrollView addConstraints:
[NSLayoutConstraint
constraintsWithVisualFormat:@"V:[previousView][categoryPostViewObj]"
options:0 metrics:nil
views:@{@"categoryPostViewObj":categoryPostViewObj, @"previousView":previousView}]];

非常感谢!

最佳答案

UIScrollView内部使用约束的最佳方式是完全不使用它们。

如果你想使用约束来布局 ScrollView 的内部,那么创建一个 UIView 来包含 ScrollView 中的所有内容,然后将这个 View 放入 ScrollView 中。

通过这样做,您现在可以使用您创建的容器 View 来放置所有约束。

关于Objective-C UIScrollView 约束,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23778690/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com