水平 ScrollView 以编程方式反弹问题

horizontal ScrollView bounces back issue programmatically

我有一个简单的 ScrollView,里面有 4 个 buttons,我想把它变成 horizontal ScrollView

我是这样设置的:

let itemScrollView: UIScrollView = {
    let v = UIScrollView()
    v.translatesAutoresizingMaskIntoConstraints = false
    v.backgroundColor = .red
    return v
}()

我这样限制它和它的内容:

itemView.addSubview(itemScrollView)
itemScrollView.addSubview(imageButton)
itemScrollView.addSubview(priceButton)
itemScrollView.addSubview(linkButton)
itemScrollView.addSubview(noteButton)
self.addSubview(dropDownButton)

itemView.heightAnchor.constraint(equalToConstant: 60).isActive = true

dropDownButton.heightAnchor.constraint(equalToConstant: 35).isActive = true
dropDownButton.widthAnchor.constraint(equalToConstant: 150).isActive = true
dropDownButton.trailingAnchor.constraint(equalTo: itemView.trailingAnchor, constant: -20).isActive = true
dropDownButton.centerYAnchor.constraint(equalTo: itemView.centerYAnchor).isActive = true

itemScrollView.topAnchor.constraint(equalTo: itemView.topAnchor).isActive = true
itemScrollView.leadingAnchor.constraint(equalTo: itemView.leadingAnchor).isActive = true
itemScrollView.trailingAnchor.constraint(equalTo: dropDownButton.leadingAnchor).isActive = true
itemScrollView.bottomAnchor.constraint(equalTo: itemView.bottomAnchor).isActive = true

imageButton.heightAnchor.constraint(equalToConstant: 25).isActive = true
imageButton.widthAnchor.constraint(equalToConstant: 25).isActive = true
imageButton.centerYAnchor.constraint(equalTo: itemScrollView.centerYAnchor).isActive = true
imageButton.leadingAnchor.constraint(equalTo: itemScrollView.leadingAnchor, constant: 20).isActive = true

priceButton.heightAnchor.constraint(equalToConstant: 25).isActive = true
priceButton.widthAnchor.constraint(equalToConstant: 25).isActive = true
priceButton.centerYAnchor.constraint(equalTo: itemScrollView.centerYAnchor).isActive = true
priceButton.leadingAnchor.constraint(equalTo: imageButton.leadingAnchor, constant: 45).isActive = true

linkButton.heightAnchor.constraint(equalToConstant: 25).isActive = true
linkButton.widthAnchor.constraint(equalToConstant: 25).isActive = true
linkButton.centerYAnchor.constraint(equalTo: itemScrollView.centerYAnchor).isActive = true
linkButton.leadingAnchor.constraint(equalTo: priceButton.leadingAnchor, constant: 45).isActive = true

noteButton.heightAnchor.constraint(equalToConstant: 25).isActive = true
noteButton.widthAnchor.constraint(equalToConstant: 25).isActive = true
noteButton.centerYAnchor.constraint(equalTo: itemScrollView.centerYAnchor).isActive = true
noteButton.leadingAnchor.constraint(equalTo: linkButton.leadingAnchor, constant: 45).isActive = true

最后看起来像这样,正是我想要的(第 4 个按钮隐藏在 dropDownButton 后面),但您只能滚动 垂直

更新:

通过添加这行代码,我可以水平滚动:

v.alwaysBounceHorizontal = true

但是现在的问题是滚动后总是"bounces back"向左。我该如何解决这个问题?

您想使您的视图控制器符合 UIScrollViewDelegate,设置 scrollView.delegate = self 并像这样实现 scrollViewDidScroll 方法:

func scrollViewDidScroll(scrollView: UIScrollView) {
    if scrollView.contentOffset.y != 0 {
        scrollView.contentOffset.y = 0
    }
}