2015-09-10 51 views
0

我想讓StackView內容可以滾動。我插入了虛擬UIImage視圖來試圖讓它工作,但它不滾動。堆棧和滾動視圖已經連接到IB。我錯過了什麼?我不能讓UIScrollView與UIStackView一起工作

import UIKit 

class ViewController: UIViewController { 

    @IBOutlet weak var menuStackView: UIStackView! 
    @IBOutlet weak var menuScrollView: UIScrollView! 
    override func viewDidLoad() { 
     super.viewDidLoad() 
     // Do any additional setup after loading the view, typically from a nib. 


     var itemImageView = UIImageView(image: UIImage(named: "splash2")) 
     itemImageView.contentMode = .ScaleAspectFit 
     self.menuStackView.addArrangedSubview(itemImageView) 

     itemImageView = UIImageView(image: UIImage(named: "splash2")) 
     itemImageView.contentMode = .ScaleAspectFit 
     self.menuStackView.addArrangedSubview(itemImageView) 

     itemImageView = UIImageView(image: UIImage(named: "splash2")) 
     itemImageView.contentMode = .ScaleAspectFit 
     self.menuStackView.addArrangedSubview(itemImageView) 

     itemImageView = UIImageView(image: UIImage(named: "splash2")) 
     itemImageView.contentMode = .ScaleAspectFit 
     self.menuStackView.addArrangedSubview(itemImageView) 

     itemImageView = UIImageView(image: UIImage(named: "splash2")) 
     itemImageView.contentMode = .ScaleAspectFit 
     self.menuStackView.addArrangedSubview(itemImageView) 

     itemImageView = UIImageView(image: UIImage(named: "splash2")) 
     itemImageView.contentMode = .ScaleAspectFit 
     self.menuStackView.addArrangedSubview(itemImageView) 

     itemImageView = UIImageView(image: UIImage(named: "splash2")) 
     itemImageView.contentMode = .ScaleAspectFit 
     self.menuStackView.addArrangedSubview(itemImageView) 


     itemImageView = UIImageView(image: UIImage(named: "splash2")) 
     itemImageView.contentMode = .ScaleAspectFit 
     self.menuStackView.addArrangedSubview(itemImageView) 


    } 

    override func viewDidLayoutSubviews() { 
     super.viewDidLayoutSubviews() 
     menuScrollView.contentSize = CGSize(width: menuStackView.frame.width, height: menuStackView.frame.height) 

    } 
} 

回答

1

我的想法是創建一個UIView,你設置你的滾動視圖。然後將堆棧視圖添加到該內容視圖。因爲不然的話,爲了使堆棧視圖像你想要的那樣工作,我必須在它的所有面上添加約束到滾動視圖並添加高度和寬度約束。

我也回答了在這裏過類似的問題,這可能有助於:

https://stackoverflow.com/a/32999501/4626443

而這裏的鏈接到一個項目,一個工作滾動stackview,你可以用它來獲得再次去:

https://github.com/Rnorback/ScrollingStackView