[英]UIStackView programmatically filled with UIViews doesn't scroll inside UIScrollView
I'm still a SO and Swift newbie, so please, be patient :-) 我还是SO和Swift的新手,所以请耐心等候:-)
I have a UIViewController
. 我有一个UIViewController
。
Inside this viewcontroller
there's a UIScrollView
. 在该viewcontroller
内部,有一个UIScrollView
。
Inside the UIScrollView
there's a UIStackView
(pinned with AutoLayout
at 0,0,0,0 and centered vertically and horizontally), with horizontal axis. 在UIScrollView
有一个UIStackView
(固定在0,0,0,0处的AutoLayout
并在垂直和水平方向上居中),并带有水平轴。
My goal is to add subviews to the stackview and make the scrollview scroll horizontally. 我的目标是将子视图添加到stackview,并使scrollview水平滚动。
So, I did the following: 因此,我做了以下工作:
import UIKit
class ViewController: UIViewController, UIScrollViewDelegate {
@IBOutlet weak var scroller: UIScrollView!
@IBOutlet weak var stack: UIStackView!
override func viewDidLoad() {
super.viewDidLoad()
scroller.delegate = self
for i in 0..<10 {
let customView = UIView(frame: CGRect(x: 0, y: 0, width: 240, height: 128))
// .random is unimportant here
customView.backgroundColor = .random()
stack.addArrangedSubview(customView)
}
}
override func viewDidLayoutSubviews(){
self.scroller.contentSize = CGSize(width:2400,height:128)
print(self.scroller.contentSize) // 2400
print(self.stack.subviews)
}
func scrollViewDidScroll(_ scrollView: UIScrollView) {
print("scrollin'")
}
}
Problem is there is something I must be missing because the scrollview doesn't scroll at all. 问题是我必须缺少某些东西,因为scrollview根本不滚动。 I only see one view (of course, anytime with a different color). 我只能看到一个视图(当然,任何时候都可以使用不同的颜色)。
I'm sure the views are loaded because this is second print output: 我确定视图已加载,因为这是第二个打印输出:
[<UIView: 0x159e01a10; frame = (0 0; 240 128); layer = <CALayer: 0x28115d7a0>>, <UIView: 0x159e03f10; frame = (0 0; 240 128); layer = <CALayer: 0x28115d800>>, <UIView: 0x159e046d0; frame = (0 0; 240 128); layer = <CALayer: 0x28115d960>>, <UIView: 0x159e0e410; frame = (0 0; 240 128); layer = <CALayer: 0x28115da80>>, <UIView: 0x159e0e7c0; frame = (0 0; 240 128); layer = <CALayer: 0x28115db20>>, <UIView: 0x159e0e9a0; frame = (0 0; 240 128); layer = <CALayer: 0x28115da60>>, <UIView: 0x159e0eb80; frame = (0 0; 240 128); layer = <CALayer: 0x28115dbe0>>, <UIView: 0x159e0ed60; frame = (0 0; 240 128); layer = <CALayer: 0x28115dc80>>, <UIView: 0x159e0ef40; frame = (0 0; 240 128); layer = <CALayer: 0x28115dd20>>, <UIView: 0x159e0f120; frame = (0 0; 240 128); layer = <CALayer: 0x28115ddc0>>]
So, what is it happening? 那么,这是怎么回事? Why the scrollview doesn't scroll? 为什么scrollview不滚动?
Replace this 取代这个
let customView = UIView(frame: CGRect(x: 0, y: 0, width: 240, height: 128))
with 与
let customView = UIView(frame: CGRect(x: CGFloat( i * 240 ) , y: 0, width: 240, height: 128))
I see no need for the stackView , and 我认为不需要stackView,并且
scroller.addSubview(customView)
try this code, i hope it helps you. 试试这个代码,希望对您有所帮助。
override func viewDidLoad() {
super.viewDidLoad()
scroller.delegate = self
for i in 0..<10 {
let xPosition = 240 * CGFloat(i)
let customView = UIView(frame: CGRect(x: xPosition, y: 0, width: 240, height: 128))
// .random is unimportant here
customView.backgroundColor = .random()
scroller.addSubview(customView)
}
scroller.contentSize.width = 2400
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.