gpt4 book ai didi

ios 在 Masonry 中设置 uiscrollview 的 contentsize

转载 作者:行者123 更新时间:2023-12-01 18:47:34 27 4
gpt4 key购买 nike

我使用纯代码(不是 Storyboard或 xib)来设置所有 View 的大小,我不想使用硬代码来设置 frame,所以我使用 Masonry。

现在我有一个 ScrollView 和一组 View 作为 ScrollView 的 subview ,代码是这样的:

[self.scrollview addSubview:[self.imageviews objectAtIndex:0]];
[[self.imageviews objectAtIndex:0] mas_makeConstraints:^(MASConstraintMaker *make) {
make.size.equalTo(self.scrollview);
make.top.equalTo(@0);
make.left.equalTo(@0);
}];
for (NSInteger i = 1; i < maxcnt ; i++) {
[self.scrollview addSubview:[self.imageviews objectAtIndex:i]];
UIImageView *previous = [self.imageviews objectAtIndex:i-1];
[[self.imageviews objectAtIndex:i] mas_makeConstraints:^(MASConstraintMaker *make) {
make.size.equalTo(self.scrollview);
make.top.equalTo(@0);
make.left.equalTo(previous.mas_right);
}];
}

但是问题来了:

1) 如何使用 Masonry 设置 contentsize的 ScrollView ?以下代码无法正常运行,因为在旋转事件中, self.view.frame.size.width改变。
self.scrollview.contentSize = CGSizeMake(self.imageviews.count * self.view.frame.size.width, self.view.frame.size.height);

2)如果我必须使用上面的代码来设置,如何处理旋转事件所以 contentsize更新得很好。

最佳答案

你快到了。您所要做的就是在最后一个 ImageView 上设置一个正确的约束。通过这样做,您可以在 contentView 的左侧和右侧之间创建一个“连接”。这样 UIScrollView “知道” subview 需要多少空间并调整 contentSize因此。

无需设置contentSize一点也不。自动布局会为您处理此问题,并且在旋转设备时也可以使用。

我写了一个blog post关于这个不久前。它使用 SnapKit,但这基本上只是“Masonry with Swift”的另一个名称。语法几乎相同。它描述了一个垂直 ScrollView ,但这个想法在水平 ScrollView 上是相同的。

还有一件小事:

而不是这样做:

make.top.equalTo(self.scrollview.mas_top);

你可以这样做:
make.top.equalTo(self.scrollview);

关于ios 在 Masonry 中设置 uiscrollview 的 contentsize,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34130922/

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