我的 UIScollView 垂直滚动有问题。我只想让我的 ScollView 水平滚动。我试图改变 contentSize 但这不起作用。
我使用约束,这段代码:
var plugeeNews : [Plugs]?
var topNewsScrollView : UIScrollView = {
var scrollView = UIScrollView(frame: CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: 75))
scrollView.isPagingEnabled = true
scrollView.showsHorizontalScrollIndicator = false
scrollView.showsVerticalScrollIndicator = false
scrollView.backgroundColor = .gray
scrollView.isDirectionalLockEnabled = true
scrollView.translatesAutoresizingMaskIntoConstraints = false
return scrollView
}()
func setupViewInTopNewsScrollView() {
for i in 0..<plugeeNews.count {
let xPosition = screenSize.width * CGFloat(i)
let myView = TopNewsCell(frame: CGRect(x: xPosition, y: 0, width: screenSize.width , height: heightOfNewsCell))
myView.label.text = plugeeNews[i]
myView.randomBackgroundColor(number: i)
topNewsScrollView.contentSize = CGSize(width: topNewsScrollView.frame.width * CGFloat(i + 1),height: heightOfNewsCell)
topNewsScrollView.addSubview(myView)
}
}
这是我的 UIScrollView 的高度约束:
...
topNewsScrollView.heightAnchor.constraint(equalToConstant: heightOfNewsCell).isActive = true
这是结果:
而且我不希望 UIScollView 上的白色/灰色视图下方的灰色视图,我只想在我的视图之间水平滑动。