【发布时间】:2020-05-14 13:13:33
【问题描述】:
感觉这个问题的一些变体肯定已经被问到并得到了回答,但我找不到它。
我终于硬着头皮从故事板转移到以编程方式创建的视图层次结构,但遇到了障碍。在视图控制器的 viewDidLoad() 中,我成功添加了一个子视图,我在变量 managedView 中保留了对它的引用。紧接着(在同一个 viewDidLoad 中),我试图添加一个 UILabel 作为 managedView 的子视图。但是标签没有出现。没有错误信息,没有失败,没有标签……
我已使用视图调试器并确认标签不存在但不可见或不在屏幕上的情况。它不在视图层次结构中。
我错过了什么?
这是 viewDidLoad 中的代码
override func viewDidLoad()
{
super.viewDidLoad()
view.backgroundColor = .clear
managedView = UIView()
view.addSubview(managedView)
managedView.translatesAutoresizingMaskIntoConstraints = false
managedView.backgroundColor = .systemBackground
managedView.layer.cornerRadius = 10
managedView.layer.masksToBounds = true
managedView.layer.borderColor = UIColor.gray.cgColor
managedView.layer.borderWidth = 1.0
managedView.alignCenter(to: view)
managedView.minSize(width:200.0, height:200.0)
let titleView:UILabel =
{
let v = UILabel()
v.translatesAutoresizingMaskIntoConstraints = false
v.text = title ?? "???"
v.font = Style.titleFont
v.backgroundColor = .systemYellow
v.textColor = UIColor.label
return v
}()
managedView.addSubview(titleView)
titleView.anchorTop(to: managedView,offset: Style.edgeMargin)
titleView.constrainSize(width:150.0, height:15.0)
titleView.alignCenterX(to: managedView)
}
请注意,我扩展了 UIView 以提供更简单的命名自动布局约束方法。下面是上面用到的约束方法的代码:
extension UIView
{
...
func alignCenterX(to otherView:UIView, offset:CGFloat = 0.0)
{
self.centerXAnchor.constraint(equalTo: otherView.centerXAnchor, constant: offset).isActive = true
}
...
func anchorTop(to otherView:UIView, offset:CGFloat = 0.0)
{
self.topAnchor.constraint(equalTo: otherView.bottomAnchor, constant: offset).isActive = true
}
...
func alignCenterY(to otherView:UIView, offset:CGFloat = 0.0)
{
self.centerYAnchor.constraint(equalTo: otherView.centerYAnchor, constant: offset).isActive = true
}
...
func constrainWidth(_ width:CGFloat)
{
self.widthAnchor.constraint(equalToConstant: width).isActive = true
}
func constrainHeight(_ height:CGFloat)
{
self.heightAnchor.constraint(equalToConstant: height).isActive = true
}
...
func minWidth(_ width:CGFloat)
{
self.widthAnchor.constraint(greaterThanOrEqualToConstant: width).isActive = true
}
func minHeight(_ height:CGFloat)
{
self.heightAnchor.constraint(greaterThanOrEqualToConstant: height).isActive = true
}
...
func alignCenter(to otherView:UIView)
{
alignCenterY(to: otherView)
alignCenterX(to: otherView)
}
func minSize(width:CGFloat,height:CGFloat)
{
self.minWidth(width)
self.minHeight(height)
}
}
【问题讨论】:
-
constrainSize在哪里? -
除了
minSize之外,您是否对managedView添加了任何约束? -
Doh... 问这个问题时不小心把它删掉了。 constrainSize() 只是对 constrainWidth() 和 constrainHeight() 的包装
-
alignCenter(to:view) 添加约束以将托管视图的 centerXAnchor 和 centerYanchor 分别等同于视图的 centerXAnchor 和 centerYanchor。
-
我明白了...期待:) ...除了 minSize 之外,您是否向 managedView 添加了任何约束?这个呢?