2014-08-27 57 views
0

我想將2個按鈕移動到屏幕寬度中心。 它應該看起來像:| < - (100)FirstButton(50) - > SecondButton(100) - > |NSLayoutConstraint不起作用

我從第一個按鈕開始。

var const = NSLayoutConstraint(item: firstButton, 
    attribute: NSLayoutAttribute.Left, 
    relatedBy: NSLayoutRelation.Equal, 
    toItem: view.superview, 
    attribute: NSLayoutAttribute.Left, 
    multiplier: 1.0, 
    constant: 100) 

爲什麼它不起作用?

+0

Cou ld它是你沒有添加任何約束的垂直位置。你添加的只是水平的。 – Sandeep 2014-08-27 15:10:13

+0

我做到了。我在IB中制定了水平和垂直的約束條件 – 2014-08-27 15:31:17

+0

我可以在Swift中閱讀關於VFL的內容嗎? – 2014-08-27 16:06:48

回答

0

它似乎firstButton尚未財產translatesAutoresizingMaskIntoConstraints設置爲false

translatesAutoresizingMaskIntoConstraints蘋果文檔的描述:

/* By default, the autoresizing mask on a view gives rise to constraints that fully determine the view's position. This allows the auto layout system to track the frames of views whose layout is controlled manually (through -setFrame:, for example). When you elect to position the view using auto layout by adding your own constraints, you must set this property to NO. IB will do this for you. */ @available(iOS 6.0, *) open var translatesAutoresizingMaskIntoConstraints: Bool // Default YES

所以你必須設置firstButtontranslatesAutoresizingMaskIntoConstraints爲假,當你加入你自己的約束