2017-02-18 91 views
0

我是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

回答

2

這是一個觀點的約束與高度相等的屏幕底部的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

0

你有兩個問題:

  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

相關問題