[英]Horizontal UIStackView inside UIScrollView not scrolling
確保UIScrollView的內容大小設置正確。 以下是一些有用的參數。
scrollVIew.contentInset scrollVIew.scrollIndicatorInsets scrollView.contentSize
當內容視圖比滾動視圖高時,滾動視圖將啟用垂直滾動。 當內容視圖比滾動視圖寬時,滾動視圖將啟用水平滾動。 否則,默認情況下將禁用滾動。 您必須動態設置內容視圖的大小,以便當它們更改字體時,內容視圖的寬度大於滾動視圖的寬度。 您可以將堆棧視圖包裝在另一個UIViewController中,並將其視為內容視圖。
問題似乎是滾動視圖和右側按鈕之間的大於或等於約束。 我將其更改為“相等”,並且有效。
嘗試在我這一邊工作正常並滾動良好。
func createHorizontalStackViewWithScroll() {
self.view.addSubview(stackScrollView)
stackScrollView.translatesAutoresizingMaskIntoConstraints = false
stackScrollView.heightAnchor.constraint(equalToConstant: 85).isActive = true
stackScrollView.leadingAnchor.constraint(equalTo: self.view.leadingAnchor).isActive = true
stackScrollView.trailingAnchor.constraint(equalTo: self.view.trailingAnchor).isActive = true
stackScrollView.bottomAnchor.constraint(equalTo: visualEffectViews.topAnchor).isActive = true
stackScrollView.addSubview(stackView)
stackView.translatesAutoresizingMaskIntoConstraints = false
stackView.topAnchor.constraint(equalTo: stackScrollView.topAnchor).isActive = true
stackView.leadingAnchor.constraint(equalTo: stackScrollView.leadingAnchor).isActive = true
stackView.trailingAnchor.constraint(equalTo: stackScrollView.trailingAnchor).isActive = true
stackView.bottomAnchor.constraint(equalTo: stackScrollView.bottomAnchor).isActive = true
stackView.heightAnchor.constraint(equalTo: stackScrollView.heightAnchor).isActive = true
stackView.distribution = .equalSpacing
stackView.spacing = 5
stackView.axis = .horizontal
stackView.alignment = .fill
for i in 0 ..< images.count {
let photoView = UIButton.init(frame: CGRect(x: 0, y: 0, width: 85, height: 85))
// set button image
photoView.translatesAutoresizingMaskIntoConstraints = false
photoView.heightAnchor.constraint(equalToConstant: photoView.frame.height).isActive = true
photoView.widthAnchor.constraint(equalToConstant: photoView.frame.width).isActive = true
stackView.addArrangedSubview(photoView)
}
stackView.setNeedsLayout()
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.