如何使用 VLF 将对象一个堆叠在另一个之上?
How to use VLF to stack objects one on top of the other?
出于某种原因,此 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)
正在生产这个:
我似乎无法理解的是为什么第一个对象 chapterVerseLabel
之间存在巨大差距。理想情况下,我希望对象堆叠在另一个之上。也许之间有 10pt 的差距。这就是我假设这条线会做的事情:
allConstraints += NSLayoutConstraint.constraintsWithVisualFormat("V:|-[chapterVerseLabel]-[sanskritLabel]-[englishLabel]-[translationLabel]-[commentaryLabel]-|", options: [], metrics: nil, views: views)
我也尝试通过添加指定的高度等进行变化,但 none 似乎有效。
由于您的所有视图都是垂直锚定的,因此自动布局必须放宽至少一个限制才能根据您的设备尺寸显示它们。
它不知道你最好在底部留更多的空隙,所以你必须指定它。
尝试为将最后一个 UILabel 附加到视图控制器底部的约束分配较低的优先级。
这是一个指定优先级的示例:
NSDictionary *metrics = @{@"lowPriority":@(UILayoutPriorityDefaultLow)};
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"|-30.0@lowPriority-[label]" options:0 metrics:metrics views:views];
出于某种原因,此 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)
正在生产这个:
我似乎无法理解的是为什么第一个对象 chapterVerseLabel
之间存在巨大差距。理想情况下,我希望对象堆叠在另一个之上。也许之间有 10pt 的差距。这就是我假设这条线会做的事情:
allConstraints += NSLayoutConstraint.constraintsWithVisualFormat("V:|-[chapterVerseLabel]-[sanskritLabel]-[englishLabel]-[translationLabel]-[commentaryLabel]-|", options: [], metrics: nil, views: views)
我也尝试通过添加指定的高度等进行变化,但 none 似乎有效。
由于您的所有视图都是垂直锚定的,因此自动布局必须放宽至少一个限制才能根据您的设备尺寸显示它们。 它不知道你最好在底部留更多的空隙,所以你必须指定它。 尝试为将最后一个 UILabel 附加到视图控制器底部的约束分配较低的优先级。 这是一个指定优先级的示例:
NSDictionary *metrics = @{@"lowPriority":@(UILayoutPriorityDefaultLow)};
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"|-30.0@lowPriority-[label]" options:0 metrics:metrics views:views];