【发布时间】:2018-02-06 06:19:33
【问题描述】:
我对 UILayoutGuide 完全陌生。我想在 imageView 下方的空白区域中间放置一个 UILabel,它是 imageView 和 UILabel 的父视图(参见下面的视图层次结构)。
通常,我会添加另一个视图并将顶部约束设置为 UIImageView 并将底部约束设置为底部边缘,然后将标签水平和垂直居中于视图。
但我决定使用 UILayoutGuide 来实现它。这是我的代码:
override func awakeFromNib() {
super.awakeFromNib()
let topGuide = UILayoutGuide()
let bottomGuide = UILayoutGuide()
self.view.addLayoutGuide(topGuide)
self.view.addLayoutGuide(bottomGuide)
topGuide.bottomAnchor.constraint(equalTo: title.topAnchor).isActive = true
bottomGuide.topAnchor.constraint(equalTo: title.bottomAnchor).isActive = true
self.view.layoutIfNeeded()
}
结果是我的 UILabel 根本不显示。我做错了什么?
【问题讨论】:
-
想要使用情节提要还是以编程方式?
-
Interface Builder 中没有布局指南,那么屏幕截图是做什么用的?另请注意,屏幕截图告诉您您的约束是非法的。
标签: ios swift uicollectionview uilayoutguide