2016-03-11 44 views
0

出於某種原因,這個VLF代碼..:如何使用VLF堆疊堆疊物體?

// Label Constraints 
    var allConstraints = [NSLayoutConstraint]() 
    let views = ["chapterVerseLabel" : chapterVerseLabel, "sanskritLabel" : sanskritLabel, "englishLabel" : englishLabel, "translationLabel" : translationLabel, "commentaryLabel" : commentaryLabel] 
    allConstraints += NSLayoutConstraint.constraintsWithVisualFormat("V:|-[chapterVerseLabel]-[sanskritLabel]-[englishLabel]-[translationLabel]-[commentaryLabel]-|", options: [], metrics: nil, views: views) 
    allConstraints += NSLayoutConstraint.constraintsWithVisualFormat("H:|-15-[chapterVerseLabel]-15-|", options: [], metrics: nil, views: views) 
    allConstraints += NSLayoutConstraint.constraintsWithVisualFormat("H:|-15-[sanskritLabel]-15-|", options: [], metrics: nil, views: views) 
    allConstraints += NSLayoutConstraint.constraintsWithVisualFormat("H:|-15-[englishLabel]-15-|", options: [], metrics: nil, views: views) 
    allConstraints += NSLayoutConstraint.constraintsWithVisualFormat("H:|-15-[translationLabel]-15-|", options: [], metrics: nil, views: views) 
    allConstraints += NSLayoutConstraint.constraintsWithVisualFormat("H:|-15-[commentaryLabel]-15-|", options: [], metrics: nil, views: views) 
    NSLayoutConstraint.activateConstraints(allConstraints) 

是生產這樣的:

enter image description here

似乎我無法理解的是爲什麼有第一對象之間的巨大差距chapterVerseLabel。理想情況下,我希望將對象堆疊在另一個之上。可能是10點之間的保證金。這是我認爲這條線會做的:

allConstraints += NSLayoutConstraint.constraintsWithVisualFormat("V:|-[chapterVerseLabel]-[sanskritLabel]-[englishLabel]-[translationLabel]-[commentaryLabel]-|", options: [], metrics: nil, views: views) 

我試着通過增加指定的高度等變化,但似乎沒有工作。

回答

1

由於所有視圖都是垂直錨定的,因此自動佈局必須放鬆至少一個約束,以便根據設備大小顯示它們。 它不知道你最好留下更多的差距,所以你必須指定它。 嘗試將較低的優先級分配給將最後一個UILabel附加到視圖控制器底部的約束。 下面是一個指定優先級的示例:

NSDictionary *metrics = @{@"lowPriority":@(UILayoutPriorityDefaultLow)}; 
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"|[email protected][label]" options:0 metrics:metrics views:views];