我试图添加固定到其超级视图的前导/尾随/顶部/底部的子视图,但是当我尝试激活约束时,都没有绘制视图。
当我注释掉约束代码时,我看到蓝色视图,激活它们我只是在操场上看到一个黑色视图。
为了帮助调试此问题,我创建了一个游乐场,但仍然无法弄清楚为什么会这样:
//: A UIKit based Playground for presenting user interface
import UIKit
import PlaygroundSupport
class MyView: UIView
{
override init(frame: CGRect)
{
super.init(frame: frame)
translatesAutoresizingMaskIntoConstraints = false
backgroundColor = .blue
}
required init?(coder aDecoder: NSCoder)
{
fatalError()
}
}
// Present the view controller in the Live View window
let myView = MyView(frame: CGRect(x: 0, y: 0, width: 300, height: 200))
PlaygroundPage.current.liveView = myView
let subview = UIView()
subview.translatesAutoresizingMaskIntoConstraints = false
subview.backgroundColor = .red
myView.addSubview(subview)
subview.leadingAnchor.constraint(equalTo: myView.leadingAnchor).isActive = true
subview.trailingAnchor.constraint(equalTo: myView.trailingAnchor).isActive = true
subview.topAnchor.constraint(equalTo: myView.topAnchor).isActive = true
subview.bottomAnchor.constraint(equalTo: myView.bottomAnchor).isActive = true
答案 0 :(得分:0)
因此仅设置框架是不够的,superview必须设置宽度和高度约束。