【发布时间】:2019-12-07 11:07:56
【问题描述】:
我的 UIViewController 遇到了一个奇怪的行为,它包含一个 UIScrollView。 ViewController 是从另一个 ViewController 推送而来的。视图的布局如下
- UI 视图
- UIScrollView
- UI 视图
- UIImageVIew
- UILabel
- UIStackView
- UIImageVIew
- UI 视图
- UIScrollView
我设置约束和更新stackView内容的方式如下。
private extension PresentationViewController {
func setupView() {
isScrolling = true
view.addSubview(scrollView)
scrollView.snp.makeConstraints { (builder) in
builder.top.left.bottom.right.equalToSuperview()
}
scrollView.addSubview(contenView)
contenView.snp.makeConstraints { (builder) in
builder.top.left.bottom.right.equalToSuperview()
builder.width.equalToSuperview()
}
contenView.addSubview(headerImageView)
headerImageView.snp.makeConstraints { (builder) in
builder.top.left.right.equalToSuperview()
builder.height.equalTo(view.frame.height / 2)
}
headerImageView.addSubview(headerTitleLabel)
headerTitleLabel.snp.makeConstraints { (builder) in
builder.left.right.equalToSuperview()
builder.centerX.equalToSuperview()
builder.centerY.equalToSuperview()
}
contenView.addSubview(presentationStackView)
presentationStackView.snp.makeConstraints { (builder) in
builder.top.equalTo(headerImageView.snp.bottom).inset(-Margins.xxLarge)
builder.left.right.equalToSuperview()
builder.bottom.equalTo(scrollView.snp.bottom)
}
}
func updateView(presentationResponse: PresentationResponse?) {
guard let presentationResponse = presentationResponse else { return }
let firstPresentation = presentationResponse.presentationItems[0]
titleView.title = presentationResponse.galleryName
headerImageView.sd_setImage(with: URL(string: firstPresentation.imageSet.fullSize ?? ""), placeholderImage: nil)
headerTitleLabel.text = presentationResponse.galleryName
for item in presentationResponse.presentationItems.dropFirst() {
let sectionImageView = UIImageView()
sectionImageView.contentMode = .scaleAspectFit
sectionImageView.clipsToBounds = true
let sectionCaptionLabel = BaseLabel(withConfiguration: .headline)
sectionCaptionLabel.text = item.rowCaption
let sectionView = UIView()
sectionView.addSubview(sectionImageView)
sectionView.addSubview(sectionCaptionLabel)
sectionImageView.sd_setImage(with: URL(string: item.imageSet.defaultImage ?? "")) { [weak self, weak sectionView] (image, error, _, _) in
guard let strongSelf = self,
let sectionView = sectionView,
let image = image else { return }
strongSelf.presentationStackView.addArrangedSubview(sectionView)
sectionImageView.snp.makeConstraints { (builder) in
builder.top.left.bottom.equalToSuperview().inset(Margins.medium)
let width:CGFloat = strongSelf.view.frame.size.width / 3
builder.width.equalTo(width)
builder.height.equalTo(width * (1 / image.aspectRatioValue))
}
sectionCaptionLabel.snp.makeConstraints { (builder) in
builder.left.equalTo(sectionImageView.snp.right).inset(-Margins.medium)
builder.top.right.equalToSuperview().inset(Margins.medium)
}
}
}
}
}
当对后端的异步操作完成时,在 ViewDidLoad 和 updateView(:) 中触发 setupView()。
您可能已经看到,我使用 Snapkit 来进行约束。 使用调试器工具检查我的 UI 时,我收到以下消息:
Scrollable content size is ambiguous for UIScrollView
这个问题似乎与 UIStackView 有关,因为它没有显示。 但是,如果我返回并再次推送,则不会出现错误,并且一切都正确显示。
【问题讨论】:
标签: swift uiscrollview uikit uistackview snapkit