addArrangedSubview代码有什么问题?

时间:2016-08-21 14:12:07

标签: swift uistackview

我正在尝试了解addArrangedSubview是如何工作的,所以我尝试了一个虚拟项目。这是我的VC:

import UIKit

class ViewController: UIViewController {

@IBOutlet weak var stackOutlet: UIStackView!
override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
    let newView = UIView(frame: CGRect(x: 0, y: 0, width: 200, height: 200))
    newView.backgroundColor = UIColor.darkGray

    let newView2 = UIView(frame: CGRect(x: 0, y: 0, width: 200, height: 200))
    newView2.backgroundColor = UIColor.blue
    stackOutlet.addArrangedSubview(newView)
    stackOutlet.addArrangedSubview(newView2)
}

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


}

当我启动应用程序时,什么都没有出现。我想我必须在stackView中设置一些约束?

1 个答案:

答案 0 :(得分:4)

好的,所以每个人都要这样做:你需要使用这个=

mapView.heightAnchor.constraint(equalToConstant: 200).isActive = true
mapView.widthAnchor.constraint(equalToConstant: 200).isActive = true