如何以编程方式添加带动态宽度的约束

问题描述:

我是iOS开发新手。我想构建没有故事板或xib/nib的布局。所以我试图以编程方式添加约束。 我以编程方式搜索了一些关于添加约束的教程。但视图无法正确显示。如何以编程方式添加带动态宽度的约束

我在我的ViewController类尝试这种代码:

override func viewDidLoad() { 
    super.viewDidLoad() 
    // Do any additional setup after loading the view, typically from a nib. 
    let testView = UIView() 
    self.view.addSubview(testView) 

    // Add Constraints 
    self.view.translatesAutoresizingMaskIntoConstraints = false 
    testView.translatesAutoresizingMaskIntoConstraints = false 

    let top = NSLayoutConstraint(item: testView, attribute: .top, relatedBy: .equal 
     , toItem: self.view, attribute: .top, multiplier: 1, constant: 50.0) 
    let bottom = NSLayoutConstraint(item: testView, attribute: .bottom, relatedBy: .equal 
     , toItem: self.view, attribute: .bottom, multiplier: 1, constant: -50.0) 
    let leading = NSLayoutConstraint(item: testView, attribute: .leading, relatedBy: .greaterThanOrEqual, toItem: self.view, attribute: .leading, multiplier: 1, constant: 50.0) 
    let trailing = NSLayoutConstraint(item: testView, attribute: .trailing, relatedBy: .greaterThanOrEqual, toItem: self.view, attribute: .trailing, multiplier: 1, constant: -50.0) 
    self.view.addConstraints([top, bottom, leading, trailing]) 
} 

一般情况下,他们并不需要定义的视图或约宽度和高度教程限制大小。但是这些视图可以在他们的教程中显示。在我的情况下,我的testView无法在应用程序中显示它,甚至设置了顶部,底部,前导和尾随约束。我错过了什么吗?有什么问题?

教程之一: http://www.knowstack.com/swift-nslayoutconstraint-programatically-sample-code/

编辑: 让我解释更多。我想创建一个适用于通用设备的UIViewUIViewconstant: 10的顶部,底部,前导和尾随限制。所以,我不需要设置大小UIView

Expected Result (I am using draw tool to simulate the result)

+0

欢迎来到SO。在询问之前,你是否已经检查过所有类似的问题?像这样的例子有大量的答案和解释:http://stackoverflow.com/questions/26180822/swift-adding-constraints-programmatically – 2017-02-18 16:54:13

+0

我之前检查过这个问题。 UIView的大小已被定义为宽度:100,高度:100在最高速率的答案。我更新了我的问题。请检查,谢谢。 – kk777

+0

尝试在添加约束后添加view.layoutIfNeeded,如果没有其他解决方案适用于您或此处有多个答案。 – 2017-02-19 07:31:25

这是一个观点的约束与高度相等的屏幕底部的80个例子:

var yourView = UIView() 
yourView.translateAutoresizingMaskIntoConstraints = false 
yourSuperView.addSubview(yourView) 

// Pin the leading edge of yourView to the leading edge of the main view 
yourView.leadingAnchor.constraintEqualToAnchor(view.leadingAnchor).active = true 

// Pin the trailing edge of yourView to the leading trailing edge 
yourView.trailingAnchor.constraintEqualToAnchor(view.trailingAnchor).active = true 

// Pin the bottomedge of yourView to the margin's leading edge 
yourView .bottomAnchor.constraintEqualToAnchor(view.bottomAnchor).active = true 

// The height of your view 
yourView.heightAnchor.constraintEqualToConstant(80).active = true 
+0

避免发布您的答案的多个问题的确切副本。如果您觉得问题是重复的,您也可以发布评论并链接到答案。 – 2017-02-18 17:23:46

你有两个问题:

  1. 不要设置为self.view.translatesAutoresizingMaskIntoConstraints = false。您只需要为要添加的子视图设置translatesAutoresizingMaskIntoConstraints

  2. 您正在使用.greaterThanOrEqual约束条件而不是.equal约束条件。问题在于留下了许多摆动空间,并且您获得的值会导致您的testView的宽度为0。如果更改为.equal将正确限制这些值。

+0

我删除'self.view.translatesAutoresizingMaskIntoConstraints = false'并将'.greaterThanOrEqual'更改为'.equal'后,'testView'仍然不显示在我的屏幕上 – kk777

+0

将更新的代码追加到问题中,但不要删除原始代码。您是否已将.greaterThanOrEquals更改为.equal? – vacawama