我在swift操场上添加了uiview约束,但约束没有';我不工作,为什么?

我在swift操场上添加了uiview约束,但约束没有';我不工作,为什么?,swift,uiview,autolayout,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 set

为什么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

删除

my1.translatesAutoresizingMaskIntoConstraints=false
my2.translatesAutoresizingMaskIntoConstraints=false

如果使用CGRect,则无需
翻译自动调整大小GMaskintoConstraints
。您可以将其用于自动布局约束

我使您的示例生效。有关解释,请参阅内联注释:

// 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

注意事项:

  • 自iOS 8以来,启用约束的正确方法是激活约束,而不是将约束添加到视图中。激活它们会告诉iOS将它们添加到适当的视图中;如果您自己将它们添加到视图中,则必须小心地将它们添加到正确的视图中。您的
    cons2
    应该添加到
    my1
    而不是
    my2
    ,因为约束涉及
    my1
    ,并且它是
    my2
    的父项
  • 一旦将
    translatesAutoResizengMaskintoConstraints
    设置为
    false
    ,则会忽略
    ,因此您需要确保有约束来完全指定视图的大小
  • 不要调用
    updateConstraints
    。如果需要在操场上启动自动布局,可以在视图中调用
    layoutifneed()
  • 样式说明:尽可能让Swift解释变量的类型
  • 不要使用
    .init
    。快速的方法是只使用类/结构名。(例如,
    NSLayoutConstraint(项:…)
    而不是
    NSLayoutConstraint.init(项…)

  • 不,我试过你说的,但结果是一样的。实际上,约束从一开始就不起作用。您可以尝试使用xcode,只需复制代码并显示即可。有什么问题吗?实际上我只想更改my2(UIview)的高度和宽度及其位置。非常感谢