2017-03-28 82 views
0

我的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 

這是結果:

enter image description here

而且我不想在t上的白色/灰色視圖下方顯示灰色視圖他UIScollView,我只是想在我的視圖之間水平滑動。

+0

爲什麼你要爲你的'UIScrollView'每次添加一個新的'UIView'的'contentSize'? –

+0

你應該仔細閱讀本教程https://www.raywenderlich.com/122139/uiscrollview-tutorial –

+0

Victor:這是爲了避免創建一個新變量來保存'i'值。好的,我要去看教程,謝謝 –

回答

-1

設置內容大小:

scrollContainer.contentSize = CGSize(width:10000, height:50) 
+0

如果你將'width'設置爲'10000'當然會水平滾動,但這需要根據'UIScrollView'內'UIView'的數量正確計算,而不是以這種方式手動設置 –

+0

當然這只是一個示例 – user1707694

+0

感謝您的投票:( – user1707694