自动布局通过Swift中的代码适合父级

时间:2015-01-08 10:12:19

标签: swift uiview autolayout

我有一个视图,我通过代码创建并添加到另一个视图作为子视图。 新的superview可以随着时间的推移改变它的框架,我希望新创建的子视图相应地改变它的框架。 如何通过Swift中的代码使用自动布局?

3 个答案:

答案 0 :(得分:8)

以下是一个例子:

let view = UIView() // existing view

let subview = UIView()
subview.setTranslatesAutoresizingMaskIntoConstraints(false)

view.addSubview(subview)
view.addConstraint(NSLayoutConstraint(item: subview, attribute: .Top, relatedBy: .Equal, toItem: view, attribute: .Top, multiplier: 1.0, constant: 0.0))
view.addConstraint(NSLayoutConstraint(item: subview, attribute: .Leading, relatedBy: .Equal, toItem: view, attribute: .Leading, multiplier: 1.0, constant: 0.0))
view.addConstraint(NSLayoutConstraint(item: view, attribute: .Bottom, relatedBy: .Equal, toItem: subview, attribute: .Bottom, multiplier: 1.0, constant: 0.0))
view.addConstraint(NSLayoutConstraint(item: view, attribute: .Trailing, relatedBy: .Equal, toItem: subview, attribute: .Trailing, multiplier: 1.0, constant: 0.0))

答案 1 :(得分:1)

iOS 13,swift 5

首先,您添加此代码

    subview.translatesAutoresizingMaskIntoConstraints = false
    view.addSubview(subview)

然后,在较新的iOS版本中,有两种方法可以做到这一点。

  1. 使用NSLayoutConstraint

    NSLayoutConstraint(item: subview, attribute: .top, relatedBy: .equal, toItem: view, attribute: .top, multiplier: 1, constant: 0).isActive = true
    NSLayoutConstraint(item: subview, attribute: .bottom, relatedBy: .equal, toItem: view, attribute: .bottom, multiplier: 1, constant: 0).isActive = true
    NSLayoutConstraint(item: subview, attribute: .left, relatedBy: .equal, toItem: view, attribute: .left, multiplier: 1, constant: 0).isActive = true
    NSLayoutConstraint(item: subview, attribute: .right, relatedBy: .equal, toItem: view, attribute: .right, multiplier: 1, constant: 0).isActive = true
    
  2. 使用NSLayoutAnchor类(详细程度较低)

    subview.topAnchor.constraint(equalTo: view.topAnchor).isActive = true
    subview.bottomAnchor.constraint(equalTo: view.bottomAnchor).isActive = true
    subview.leftAnchor.constraint(equalTo: view.leftAnchor).isActive = true
    subview.rightAnchor.constraint(equalTo: view.rightAnchor).isActive = true
    

无论哪种方式,在iOS 8和更高版本上,Apple建议使用isActive()而不是直接向视图添加约束。

此外,我相信NSLayoutAnchor方法的目的是与NSLayoutConstraint相比更加简洁和可读。

答案 2 :(得分:0)

正如@rjobidon所提到的,你应该使用以下代码( Swift3

    let view = UIView() // existing view

    let subview = UIView()
    subview.setTranslatesAutoresizingMaskIntoConstraints(false)

    view.addSubview(subview)

    NSLayoutConstraint(item: subview, attribute: .top, relatedBy: .equal, toItem: view, attribute: .top, multiplier: 1.0, constant: 0.0).isActive = true
    NSLayoutConstraint(item: subview, attribute: .leading, relatedBy: .equal, toItem: view, attribute: .leading, multiplier: 1.0, constant: 0.0).isActive = true
    NSLayoutConstraint(item: view, attribute: .bottom, relatedBy: .equal, toItem: subview, attribute: .bottom, multiplier: 1.0, constant: 0.0).isActive = true
    NSLayoutConstraint(item: view, attribute: .trailing, relatedBy: .equal, toItem: subview, attribute: .trailing, multiplier: 1.0, constant: 0.0).isActive = true