简体   繁体   English

UIScrollView中的水平UIStackView不滚动

[英]Horizontal UIStackView inside UIScrollView not scrolling

I have a horizontal UIStackView with 3 buttons inside UIScrollView. 我在UIScrollView中有一个带有3个按钮的水平UIStackView。 I'd like the stack to scroll horizontally when the user changes the font size (via Accessibility). 我希望当用户更改字体大小时(通过辅助功能),堆栈可以水平滚动。

故事板

Right now, when the user increases the font size, one of the buttons gets squashed. 现在,当用户增加字体大小时,其中一个按钮会被压扁。

Here are my constraints: 这是我的约束:

UIStackView约束

UIScrollView约束

Make sure the UIScrollView's content size is set up correctly. 确保UIScrollView的内容大小设置正确。 Here are some useful parameters. 以下是一些有用的参数。

scrollVIew.contentInset scrollVIew.scrollIndicatorInsets scrollView.contentSize

When the content view is taller than the scroll view, the scroll view enables vertical scrolling. 当内容视图比滚动视图高时,滚动视图将启用垂直滚动。 When the content view is wider than the scroll view, the scroll view enables horizontal scrolling. 当内容视图比滚动视图宽时,滚动视图将启用水平滚动。 Otherwise, scrolling is disabled by default. 否则,默认情况下将禁用滚动。 You must set your content view size dynamically so when they change the font, the content view gets wider than the scroll view width. 您必须动态设置内容视图的大小,以便当它们更改字体时,内容视图的宽度大于滚动视图的宽度。 You could wrap your stack view in another UIViewController and treat it as a content view. 您可以将堆栈视图包装在另一个UIViewController中,并将其视为内容视图。

It seems like the Greater Than or Equal constraint between the scroll view and the button on the right was the problem. 问题似乎是滚动视图和右侧按钮之间的大于或等于约束。 I changed it to Equal and it worked. 我将其更改为“相等”,并且有效。

Try this working fine my side and scrolling well. 尝试在我这一边工作正常并滚动良好。

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM