2016-03-19 75 views
0

我想創建以下結果:編程方式創建約束 - 2盒

enter image description here

由於只有手動創建NSLayoutConstraints。但目前我只有一個藍色框,或者(如果我不添加UIButton)正確的紅色視圖。

let newView = UIView() 
    newView.backgroundColor = UIColor.redColor() 
    newView.translatesAutoresizingMaskIntoConstraints = false 
    view.addSubview(newView) 

    let verticalConstraint = NSLayoutConstraint(item: newView, attribute: NSLayoutAttribute.Trailing, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.Trailing, multiplier: 1, constant: 0) 
    view.addConstraint(verticalConstraint) 

    let topContraint = NSLayoutConstraint(item: newView, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.Top, multiplier: 1, constant: 0) 
    view.addConstraint(topContraint) 

    let bottomContraint = NSLayoutConstraint(item: newView, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 0) 
    view.addConstraint(bottomContraint) 

    let widthConstraint = NSLayoutConstraint(item: newView, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 200) 
    view.addConstraint(widthConstraint) 



    let disableButton = UIButton() 
    disableButton.backgroundColor = UIColor.blueColor() 
    disableButton.translatesAutoresizingMaskIntoConstraints = false 
    view.addSubview(disableButton) 

    let leadingConstraint = NSLayoutConstraint(item: disableButton, attribute: NSLayoutAttribute.Leading, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.Leading, multiplier: 1, constant: 0) 
    view.addConstraint(leadingConstraint) 

    let topBContraint = NSLayoutConstraint(item: disableButton, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.Top, multiplier: 1, constant: 0) 
    view.addConstraint(topBContraint) 

    let bottomBContraint = NSLayoutConstraint(item: disableButton, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 0) 
    view.addConstraint(bottomBContraint) 

    let trailingConstraint = NSLayoutConstraint(item: disableButton, attribute: NSLayoutAttribute.Trailing, relatedBy: NSLayoutRelation.Equal, toItem: newView, attribute: NSLayoutAttribute.Trailing, multiplier: 1, constant: 0) 
    view.addConstraint(trailingConstraint) 

任何人都可以幫我解決什麼是錯的嗎?有什麼區別,我以哪種順序手動創建約束?

+0

你有沒有嘗試設置兩個視圖之間的水平間距? –

+0

我需要嗎?當我用IB創建時,我不會創建水平間距。只有2個視圖,一個固定寬度,第二個應該通過將所有約束(前導,尾隨,頂部,底部)設置爲0來佔用其他空間。 – derdida

+0

然後第二個應該具有等於第一個固定寬度的前導空間 –

回答

2

您的代碼可以大量使用Visual Format Language更緊湊:

let newView = UIView() 
newView.backgroundColor = UIColor.redColor() 
newView.translatesAutoresizingMaskIntoConstraints = false 
view.addSubview(newView) 

let disableButton = UIButton() 
disableButton.backgroundColor = UIColor.blueColor() 
disableButton.translatesAutoresizingMaskIntoConstraints = false 
view.addSubview(disableButton) 

let views = ["newView": newView, "disableButton": disableButton] 
let c1 = NSLayoutConstraint.constraintsWithVisualFormat("V:|[newView]|", options: [], metrics: nil, views: views) 
let c2 = NSLayoutConstraint.constraintsWithVisualFormat("H:|[newView]|", options: [], metrics: nil, views: views) 
let c3 = NSLayoutConstraint.constraintsWithVisualFormat("V:|[disableButton]|", options: [], metrics: nil, views: views) 
let c4 = NSLayoutConstraint.constraintsWithVisualFormat("H:|-0-[disableButton]", options: [], metrics: nil, views: views) 
let c5 = NSLayoutConstraint(item: disableButton, attribute: .Width, relatedBy: .Equal, toItem: newView, attribute: .Width, multiplier: 0.75, constant: 0) 

NSLayoutConstraint.activateConstraints(c1 + c2 + c3 + c4) 
view.addConstraint(c5) 
  • c1告訴自動佈局,使紅色視圖填補了上海華垂直。
  • c2做同樣的水平
  • c3告訴自動佈局,使藍色按鈕填寫垂直
  • c4使得它粘到屏幕的左邊
  • c5:一些約束不能用視覺形式寫語言,因此我們需要使用不同的功能,使disableButton.width = newView.width * 0.75

我可以寫的最後一行的

NSLayoutConstraint.activateConstraints(c1 + c2 + c3 + c4 + [c5]) 

但由於某些原因,這需要很長時間才能編譯。因此,你在上面的代碼中看到了分割。

+0

感謝您的好評! – derdida

相關問題