2017-02-10 79 views
2

我試圖讓我的頭以編程方式添加約束的作品。到目前爲止,我有我的代碼如下所示:以編程方式添加主要約束程序崩潰應用程序

import UIKit 

class ViewController: UIViewController { 

    override func viewDidLoad() { 
     super.viewDidLoad() 
     // Do any additional setup after loading the view, typically from a nib. 
     //addViewStandard() 
     addConstraintsView() 
    } 

    override func didReceiveMemoryWarning() { 
     super.didReceiveMemoryWarning() 
     // Dispose of any resources that can be recreated. 
    } 


    func addConstraintsView() { 
     let someView = UIView(frame: CGRect.zero) 
     someView.backgroundColor = UIColor.blue 
     // I want to mimic a frame set of CGRect(x: 20, y: 50, width: 50, height: 50) 
     let widthConstraint = NSLayoutConstraint(item: someView, attribute: .width, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1.0, constant: 50) 
     let heightConstraint = NSLayoutConstraint(item: someView, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1.0, constant: 50) 
     let leadingConstraint = NSLayoutConstraint(item: someView, attribute: .leading, relatedBy: .equal, toItem: view, attribute: .leading, multiplier: 1.0, constant: 20) 
     someView.translatesAutoresizingMaskIntoConstraints = false 
     someView.addConstraints([widthConstraint, heightConstraint, leadingConstraint]) 
     view.addSubview(someView) 

    } 
} 

現在,當我運行的應用程序崩潰,因爲領先的約束。錯誤消息是「不可能設置視圖層次結構未準備好約束的佈局」。我在這裏做錯了什麼?我應該將約束添加到對象(本案例中的藍色框)還是將它們添加到它的超視圖?

編輯:

後更改代碼,我有:

func addConstraintsView() { 
     let someView = UIView(frame: CGRect.zero) 
     someView.backgroundColor = UIColor.blue 
     view.addSubview(someView) 
     someView.translatesAutoresizingMaskIntoConstraints = false 
     let widthConstraint = NSLayoutConstraint(item: someView, attribute: .width, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1.0, constant: 50) 
     let heightConstraint = NSLayoutConstraint(item: someView, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1.0, constant: 50) 
     let leadingConstraint = NSLayoutConstraint(item: someView, attribute: .leading, relatedBy: .equal, toItem: view, attribute: .left, multiplier: 1.0, constant: 20) 
     someView.addConstraints([widthConstraint, heightConstraint]) 
     view.addConstraints([leadingConstraint]) 
    } 

回答

2

首先,

view.addSubview(someView) 
someView.translatesAutoresizingMaskIntoConstraints = false 

約束階段之前要來。您必須在將someView添加到其超級視圖後應用約束。

另外,如果你是針對iOS的9,我建議你使用佈局錨狀

let widthConstraint = someView.widthAnchor.constraint(equalToConstant: 50.0) 
let heightConstraint = someView.heightAnchor.constraint(equalToConstant: 50.0) 
let leadingConstraint = someView.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 20.0) 
NSLayoutConstraint.activate([widthConstraint, heightConstraint, leadingConstraint]) 

這樣你就不必擔心它查看該限制適用於。

最後(並清除您的疑問),如果您不能使用佈局錨點,則應該將超前視圖的前導約束添加到視圖中。

+0

這有幫助。它正在工作!謝謝! – KexAri

+0

這是我的榮幸! –