我有一个View Controller,它的一个UIView(名为pickerView)放置在Scene Dock中。我使用Scene Dock的原因是该UIView将包含许多子视图,并且我不想弄乱情节提要。我正在使用VFL调整pickerView的大小,因此它是设备的全屏。
问题是,当我按openPickerButtonPressed时,黄色的pickerView永远不会显示。我在这里想念什么吗? VFL约束是否设置正确?
override func viewDidLoad() {
super.viewDidLoad()
self.pickerView.translatesAutoresizingMaskIntoConstraints = false
}
@IBAction func openPickerButtonPressed(_ sender: UIButton) {
guard pickerView.superview == nil else {
return
}
self.view.addSubview(pickerView)
let viewsDictionary: [String: UIView] = ["pickerView": pickerView]
view.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:|-0-[pickerView]-0-|", options: [], metrics: nil, views: viewsDictionary))
view.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:[pickerView(0)]", options: [], metrics: nil, views: viewsDictionary)) // HEIGHT
view.addConstraint(NSLayoutConstraint(item: pickerView, attribute: .centerY, relatedBy: NSLayoutConstraint.Relation(rawValue: 0)!, toItem: view, attribute: .centerY, multiplier: 1, constant: 0))
}
如何在按下openPickerButtonPressed按钮时打开pickerView UIView?