Swift 对于UIScrollView,以编程方式显示的UIView带有错误可滚动内容大小是不明确的。只是第一次
我在使用包含UIScrollView的UIViewController时遇到异常行为。 从另一个ViewController推送ViewController。视图的布局如下所示Swift 对于UIScrollView,以编程方式显示的UIView带有错误可滚动内容大小是不明确的。只是第一次,swift,uiscrollview,uikit,uistackview,snapkit,Swift,Uiscrollview,Uikit,Uistackview,Snapkit,我在使用包含UIScrollView的UIViewController时遇到异常行为。 从另一个ViewController推送ViewController。视图的布局如下所示 UIView UIScrollView UIView UIImageVIew UILabel 乌斯塔克视图 设置约束和更新stackView内容的方法如下 private extension PresentationViewController { func setupView() {
- UIView
- UIScrollView
- UIView
- UIImageVIew
- UILabel
- 乌斯塔克视图
- UIImageVIew
- UIView
- UIScrollView
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有关,因为它没有显示出来。
但是,如果我返回并再次按下,则不会出现错误,所有内容都会正确显示。只需将等高和等宽约束添加到StackView视图中,相对于主视图,而不是UIScrollView。换句话说,容器视图的约束绑定到UIScrollView的superview
在此之后,您的故事板中将不会出现警告,您可以继续为子视图添加约束。如果布局显示正确,您就不必担心
Scrollable content size is ambiguous for UIScrollView
调试视图层次结构中出现警告。但是,如果要删除它,请在堆栈视图设置中添加一行:
presentationStackView.snp.makeConstraints { (builder) in
builder.top.equalTo(headerImageView.snp.bottom).inset(-Margins.xxLarge)
builder.left.right.equalToSuperview()
builder.bottom.equalTo(scrollView.snp.bottom)
// add this line
builder.height.equalTo(0).priority(250)
}
<>这将使堆栈视图在自动布局时空的有效高度(为零)但是
250的低优先级将允许它在添加排列的子视图时垂直扩展。谢谢。我不使用故事板。所有UI都是通过编程实现的。你能把你的意思写下来吗?非常感谢。