I have a problem with my UIScollView who scrolls vertically. I just want my ScollView scroll horizontally. I've tried to change the contentSize but that doesn't worked.
I use constraints, and this code :
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)
}
}
This is the height constraint of my UIScrollView :
...
topNewsScrollView.heightAnchor.constraint(equalToConstant: heightOfNewsCell).isActive = true
And this is the result :
And I don't want the gray view below the white/gray views on the UIScollView, i just want to swipe between my views horizontally.

contentSizeof yourUIScrollVieweach time you add a newUIView?? - Victor Siglerivalue. Ah ok I'm going to read the tutorial, thanks - Mathis DelaunaycontentSizeshould be assigned only once not every time as you're doing in the above code - Victor Sigler