0
我試圖動態地將UIViewController視圖插入水平UIStackView。 UIStackView嵌入在UIScrollView中。StackView插入偏移問題
如果我試圖在我的堆棧視圖中添加一個標籤或一個按鈕,並且在下面的代碼中註釋了相同的方法,那麼一切正常。但是當我嘗試插入視圖控制器的視圖時,它無法正確地佈置組件。
這裏是我的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()
}
}
}
上有建築物,也不是在執行過程中沒有任何警告。
我錯過了什麼嗎?