如何以编程方式初始化其约束基于其超级视图边界的子视图

时间:2018-05-07 19:45:17

标签: ios swift

我有一个UIview子类,其约束依赖于其superview的边界。 在提供的示例中,当我从Interface Builder初始化我的自定义UI视图时,事物按预期定位。但是,当从viewController以编程方式初始化时,子视图在superview之前设置,并忽略它们的约束。
有一种方法可以在超视图的边界已知时初始化或更新子视图布局吗?

class CustomView: UIView {

let firstSubview = UIView()
let secondSubview = UIView()

override init(frame: CGRect) {
    super.init(frame: frame)
    initialization()
}

required init?(coder aDecoder: NSCoder) {
    super.init(coder: aDecoder)
    initialization()
}

func initialization() {

    firstSubview.backgroundColor = UIColor.black
    addSubview(firstSubview)
    firstSubview.translatesAutoresizingMaskIntoConstraints = false
    firstSubview.leadingAnchor.constraint(equalTo: self.leadingAnchor, constant: 20).isActive = true
    //....other autoLayout constraints for trailing, top and bottom anchors

    secondSubview.backgroundColor = UIColor.blue
    addSubview(secondSubview)
    secondSubview.translatesAutoresizingMaskIntoConstraints = false
    secondSubview.leadingAnchor.constraint(equalTo:self.leadingAnchor, constant: 20).isActive = true
    //....other autoLayout constraints for trailing, top and bottom anchors 
    }
}

这里是viewController

class ViewController: UIViewController {

    let firstCustomView = CustomView(frame: CGRect.zero)
    @IBOutlet weak var secondCustomView UIView! //working with autoLayout constraints from IB


    override func viewDidLoad() {
    super.viewDidLoad()

    firstCustomView.translatesAutoresizingMaskIntoConstraints = false
    view.addSubview(firstCustomView)
    firstCustomView.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 30)
    firstCustomView.topAnchor.constraint(equalTo: view.topAnchor, constant: 200)
    firstCustomView.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: -30)
    firstCustomView.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: -200)
    }
 }

编辑: 由于来自@Sulthan的评论和帮助,以下是具有适当约束激活的工作代码。

override func viewDidLoad() {
  super.viewDidLoad()

  firstCustomView.translatesAutoresizingMaskIntoConstraints = false view.addSubview(firstCustomView).isActive = true
  firstCustomView.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 30).isActive = true
  firstCustomView.topAnchor.constraint(equalTo: view.topAnchor, constant: 200).isActive = true
  firstCustomView.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: -30).isActive = true
  firstCustomView.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: -200).isActive = true 
}

2 个答案:

答案 0 :(得分:0)

您只为父视图指定宽度约束

 firstCustomView.widthAnchor.constraint(equalTo: view.widthAnchor, multiplier: 2/3).isActive = true

你必须设置其他约束,如身高和位置(领先/尾随,上/下)

你可以尝试

firstCustomView.translatesAutoresizingMaskIntoConstraints = false
firstCustomView.leadingAnchor.constraint(equalTo: view.leadingAnchor,constant:20.0)
firstCustomView.topAnchor.constraint(equalTo: view.topAnchor , constant:30.0).isActive = true
firstCustomView.widthAnchor.constraint(equalTo: view.widthAnchor, multiplier: 2/3).isActive = true    
firstCustomView.heightAnchor.constraint(equalToConstant: 200).isActive = true

答案 1 :(得分:-1)

您也可以使用自动调整遮罩:

firstCustomView.frame = frame
firstCustomView.autoresizingMask = [.flexibleWidth, .flexibleHeight]

之后,firstCustomView将正确调整大小到viewController框架。