2016-12-14 107 views
0

我試圖動態地將UIViewController視圖插入水平UIStackView。 UIStackView嵌入在UIScrollView中。StackView插入偏移問題

如果我試圖在我的堆棧視圖中添加一個標籤或一個按鈕,並且在下面的代碼中註釋了相同的方法,那麼一切正常。但是當我嘗試插入視圖控制器的視圖時,它無法正確地佈置組件。

因此,這裏是我的佈局: enter image description here

這裏是我的ViewController代碼:

import UIKit 

class ViewController: UIViewController { 
    @IBOutlet weak var stackview: UIStackView! 

    override func viewDidLoad() { 
    super.viewDidLoad() 
    // Do any additional setup after loading the view, typically from a nib. 
    } 

    override func didReceiveMemoryWarning() { 
    super.didReceiveMemoryWarning() 
    // Dispose of any resources that can be recreated. 
    } 

    @IBAction func addComponents(_ sender: Any) { 
    let componentsView = UIStoryboard.init(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "ComponentsView") 
    //componentsView.preferredContentSize = CGSize(width: 375, height: 200) 
    //let button = UIButton(type: .system) 
    //button.setTitle("Test", for: .normal) 
    stackview.insertArrangedSubview(componentsView.view, at: 1) 
    UIView.animate(withDuration: 0.5) { 
     self.stackview.layoutIfNeeded() 
    } 
    } 
} 

這裏的結果: enter image description here

上有建築物,也不是在執行過程中沒有任何警告。

我錯過了什麼嗎?

回答

0

添加我關於堆棧視圖的觀察,不知道它會在這種情況下工作與否。

如果我們使用故事板的堆棧視圖並添加一些視圖,那麼我們需要將高度作爲必填字段。 (填充 - 填充屬性)。

嘗試在添加子視圖之前首先給出子視圖的高度。比它會按預期工作