我的UIScollView有垂直滾動問題。我只想讓我的ScollView水平滾動。我試圖改變contentSize,但沒有奏效。UIScrollView 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
這是結果:
而且我不想在t上的白色/灰色視圖下方顯示灰色視圖他UIScollView,我只是想在我的視圖之間水平滑動。
爲什麼你要爲你的'UIScrollView'每次添加一個新的'UIView'的'contentSize'? –
你應該仔細閱讀本教程https://www.raywenderlich.com/122139/uiscrollview-tutorial –
Victor:這是爲了避免創建一個新變量來保存'i'值。好的,我要去看教程,謝謝 –