我在快速操场上添加了uiview约束,但是约束不起作用,为什么?

时间:2018-10-02 08:22:18

标签: swift uiview autolayout

为什么uiview约束不起作用?我添加了uiview高度和宽度以及位置关系的约束,但是它们不起作用,为什么?

import UIKit
import Foundation

class myview : UIView{
    override init(frame:CGRect)
    {
    super.init(frame: frame)
}
required init(coder: NSCoder)
{
    super.init(coder: coder)!
}
func setcolor(color : UIColor){
    self.backgroundColor=UIColor.green
}

}
let my1:myview = myview(frame: CGRect(x: 99 , y: 99, width: 99,   height: 99))
let my2:myview = myview(frame:CGRect(x:50,y:50,width:50,height:50))
my2.setcolor(color: #colorLiteral(red: 0.952941179275513, green: 0.686274528503418, blue: 0.133333340287209, alpha: 1.0))
my1.backgroundColor = UIColor.orange
my1.translatesAutoresizingMaskIntoConstraints=false
my2.translatesAutoresizingMaskIntoConstraints=false
let leadingheight=NSLayoutConstraint.init(item: my2, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1.0, constant: 120)
let leadingwidth=NSLayoutConstraint.init(item: my2, attribute: .width, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1.0, constant: 60)
my1
my2.addConstraints([leadingheight,leadingwidth])

my1.addSubview(my2)

let cons2:NSLayoutConstraint = NSLayoutConstraint.init(item: my2, attribute: .left , relatedBy: .equal, toItem: my1, attribute: .left , multiplier: 1.0, constant: 20)

my2.addConstraints([cons2])
my2.updateConstraints()
my1.updateConstraints()

my1

enter image description here

2 个答案:

答案 0 :(得分:1)

我使您的示例工作正常进行。请参阅内联注释以获取解释:

// Note: This frame will be ignored once translatesAutoresizingMaskIntoConstraint is set to false
let my1 = myview(frame: CGRect(x: 99, y: 99, width: 99, height: 99))
let my2 = myview(frame:CGRect(x: 50, y: 50, width: 50, height: 50))
my2.setcolor(color: #colorLiteral(red: 0.952941179275513, green: 0.686274528503418, blue: 0.133333340287209, alpha: 1.0))
my1.backgroundColor = UIColor.orange

// Use constraints, not frame for size of my1 and my2
my1.translatesAutoresizingMaskIntoConstraints = false
my2.translatesAutoresizingMaskIntoConstraints = false

// Frame not used, so set constraints for width and height of my1
let my1height = NSLayoutConstraint(item: my1, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1.0, constant: 200)
let my1width = NSLayoutConstraint(item: my1, attribute: .width, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1.0, constant: 200)

// Frame not used, so set constraints for width and height of my2
let leadingheight = NSLayoutConstraint(item: my2, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1.0, constant: 120)
let leadingwidth = NSLayoutConstraint(item: my2, attribute: .width, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1.0, constant: 60)

// Activate the constraints instead of adding them to a view
NSLayoutConstraint.activate([my1width, my1height, leadingheight, leadingwidth])

my1.addSubview(my2)

let cons2 = NSLayoutConstraint(item: my2, attribute: .left , relatedBy: .equal, toItem: my1, attribute: .left , multiplier: 1.0, constant: 20)

// This is how to activate a single constraint
cons2.isActive = true

my1

注释:

  1. 从iOS 8开始,启用约束的正确方法是激活约束,而不是将约束添加到视图中。激活它们会告诉iOS将它们添加到适当的视图中。如果您自己将它们添加到视图中,则必须小心将它们添加到正确视图中。您的cons2应该已添加到my1而不是my2中,因为约束涉及my1,并且它是my2的父级。
  2. translatesAutoresizingMaskIntoConstraints设置为false后,frame将被忽略,因此您需要确保有约束条件才能完全指定视图的大小。
  3. 请勿致电updateConstraints。如果需要采取任何措施使自动版式在操场上生效,则可以在视图上调用layoutIfNeeded()
  4. 样式说明:让Swift尽可能解释变量的类型。
  5. 请勿使用.init。 Swift的方法是只使用类/结构名。 (例如,NSLayoutConstraint(item:...)而不是NSLayoutConstraint.init(item...)

答案 1 :(得分:0)

删除

my1.translatesAutoresizingMaskIntoConstraints=false
my2.translatesAutoresizingMaskIntoConstraints=false

如果使用CGRect,则不需要translatesAutoresizingMaskIntoConstraints。您可以将其与AutoLayout约束一起使用