2017-01-16 51 views
0

自定义视图位于IB中,通过从库中拖出UIView对象并将其类名设置为自定义视图类名称。此自定义视图有一个子视图,该视图在init?(coder aDecoder: NSCoder)中添加。锚定约束收缩超级视图而不是放大子视图

应该如何组合锚点类型约束以及它们应该放置在何处,以便_centralLabel子视图在自定义视图中集中设置,并且其维度是自定义视图维度的25%?

如果代码是这样写的:

override func updateConstraints() { 
    if !_subviewsConstraintsAdded { 
     _centralLabel.heightAnchor.constraint(equalTo: heightAnchor, multiplier: 0.25).isActive = true 
     _centralLabel.widthAnchor.constraint(equalTo: widthAnchor, multiplier: 0.25).isActive = true 
     _centralLabel.centerXAnchor.constraint(equalTo: centerXAnchor).isActive = true 
     _centralLabel.centerYAnchor.constraint(equalTo: centerYAnchor).isActive = true 
     _subviewsConstraintsAdded = true 
    } 
    super.updateConstraints() 
} 

代码的结果是,而不是设置_centralLabel的大小是自定义视图的25%,自定义视图缩小到( 0,0),这是调用updateConstraints()时_centralLabel的大小。

回答

0

的缺失位是:

_centralLabel.translatesAutoresizingMaskIntoConstraints = false 

应位于某处这一行编程实例子视图后:在_centralLabel子视图已经扩大

_centralLabel = UILabel() 

这种修正后,并已转移到视图的中心,而视图本身保持其原始大小,形状和位置。

相关问题