ios 设置 Masonry 中 uiscrollview 的内容大小
ios set contentsize of uiscrollview in Masonry
我使用纯代码(不是故事板或 xib)来设置所有视图的大小,我不想使用硬代码来设置框架,所以我使用 Masonry。
现在我有一个scrollView和一组view作为scrollview的子view,代码是这样的:
[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
?以下代码效果不佳,因为在旋转事件中,self.view.frame.size.width
更改。
self.scrollview.contentSize = CGSizeMake(self.imageviews.count * self.view.frame.size.width, self.view.frame.size.height);
2)如果我必须使用上面的代码来设置,如何处理旋转事件以便contentsize
更新好。
你快到了。您所要做的就是对最后一个图像视图设置一个右约束。通过这样做,您在 contentView 的左侧和右侧之间创建了一个 "connection"。这样 UIScrollView "knows" 子视图需要多少 space 并相应地调整 contentSize
。
根本不需要设置contentSize
。 Auto Layout 会为你处理这个,它在旋转设备时也能工作。
我刚才写了一篇关于这个的 blog post。它使用 SnapKit,但这基本上只是 "Masonry with Swift" 的另一个名称。语法几乎相同。它描述了垂直滚动视图,但想法与水平滚动视图相同。
还有一件小事:
而不是这样做:
make.top.equalTo(self.scrollview.mas_top);
你可以这样做:
make.top.equalTo(self.scrollview);
我使用纯代码(不是故事板或 xib)来设置所有视图的大小,我不想使用硬代码来设置框架,所以我使用 Masonry。
现在我有一个scrollView和一组view作为scrollview的子view,代码是这样的:
[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
?以下代码效果不佳,因为在旋转事件中,self.view.frame.size.width
更改。
self.scrollview.contentSize = CGSizeMake(self.imageviews.count * self.view.frame.size.width, self.view.frame.size.height);
2)如果我必须使用上面的代码来设置,如何处理旋转事件以便contentsize
更新好。
你快到了。您所要做的就是对最后一个图像视图设置一个右约束。通过这样做,您在 contentView 的左侧和右侧之间创建了一个 "connection"。这样 UIScrollView "knows" 子视图需要多少 space 并相应地调整 contentSize
。
根本不需要设置contentSize
。 Auto Layout 会为你处理这个,它在旋转设备时也能工作。
我刚才写了一篇关于这个的 blog post。它使用 SnapKit,但这基本上只是 "Masonry with Swift" 的另一个名称。语法几乎相同。它描述了垂直滚动视图,但想法与水平滚动视图相同。
还有一件小事:
而不是这样做:
make.top.equalTo(self.scrollview.mas_top);
你可以这样做:
make.top.equalTo(self.scrollview);