2015-06-04 29 views
0

的自動佈局約束可以definded爲:何時在iOS AutoLayout中使用乘數?

self.view.addConstraint(NSLayoutConstraint(item: label, 
     attribute: .Bottom, 
     relatedBy: .Equal, 
     toItem: self.view, 
     attribute: .Bottom, 
     multiplier: 1, 
     constant: 0)) 

我不明白的使用情況會是怎樣使用multiplier

什麼時候在iOS AutoLayout中使用乘法器?

+5

http://stackoverflow.com/questions/26964139/增加,在乘數

enter image description here

理解乘法器功能於自動佈局使用的相對定位 –

回答

4

我常用的一個用例是當我希望一個視圖是另一個視圖寬度的30%時。它應該是這樣的:

self.view.addConstraint(NSLayoutConstraint(item: label, 
    attribute: .Width, 
    relatedBy: .Equal, 
    toItem: self.view, 
    attribute: .Width, 
    multiplier: 0.3, 
    constant: 0)) 
0

在蘋果文檔:

自動佈局計算第一項的屬性是第二項的屬性,這個乘數的乘積。除1以外的任何值都會創建比例約束。

看看這個鏈接:Auto Layout Multiplier

1

如果你有兩個視圖。並且您的要求是一個視圖總是其他視圖的一半,或者可能是您可以使用乘數的其他視圖的10%。

爲前,

enter image description here

我想在這個命題這兩視圖始終。

enter image description here

所以拖動並從一個視圖拖放到另一個,並選擇相同的高度。

bt它顯示錯誤,都查看不在同一高度。

所以選擇的高度enter image description here

現在你必須通過計算像(second_view_height/first_view_height)來改變乘數。而這樣一來,你在故事板

使用手工編碼你可以用這種方式

self.view.addConstraint(NSLayoutConstraint(item: label, 
    attribute: .Width, 
    relatedBy: .Equal, 
    toItem: self.view, 
    attribute: .Width, 
    multiplier: 0.4869, 
    constant: 0))