【问题标题】:I add the uiview constraints on swift playgrounds , but the constraints doesn't work , why?我在 swift playground 上添加了 uiview 约束,但是约束不起作用,为什么?
【发布时间】:2018-10-02 08:22:18
【问题描述】:

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

【问题讨论】:

    标签: swift uiview autolayout


    【解决方案1】:

    我使您的示例起作用。解释见内联 cmets:

    // 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。如果需要在 Playground 中启动自动布局,您可以在您的视图上调用layoutIfNeeded()
    4. 风格说明:尽可能让 Swift 解释变量的类型。
    5. 不要使用.init。 Swift 方法是只使用类/结构名称。 (例如,NSLayoutConstraint(item:...) 而不是 NSLayoutConstraint.init(item...)

    【讨论】:

      【解决方案2】:

      删除

      my1.translatesAutoresizingMaskIntoConstraints=false
      my2.translatesAutoresizingMaskIntoConstraints=false
      

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

      【讨论】:

      • 不,我按照你说的试了一下,结果还是一样。实际上,约束从一开始就不起作用。您不能尝试使用 xcode,只需复制代码并显示。有什么问题吗?
      • 其实我只是想改变my2(UIview)的高度和宽度以及它的位置
      猜你喜欢
      • 2017-07-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-02-05
      • 2016-06-28
      • 1970-01-01
      • 2015-03-09
      相关资源
      最近更新 更多