斯威夫特4 - 在自动版式的UITableViewCell切碎多行标签编程
问题描述:
请在下面找到我的代码使用自动版式布局一个UITableViewCell的UI编程:斯威夫特4 - 在自动版式的UITableViewCell切碎多行标签编程
// Profile Pic
profileView.translatesAutoresizingMaskIntoConstraints = false
profileView.widthAnchor.constraint(equalToConstant: commentImageSize + 4).isActive = true
profileView.heightAnchor.constraint(equalToConstant: commentImageSize + 4).isActive = true
profileView.topAnchor.constraint(equalTo: topAnchor, constant: borderSize).isActive = true
profileView.leadingAnchor.constraint(equalTo: leadingAnchor, constant: borderSize).isActive = true
profileImageView.translatesAutoresizingMaskIntoConstraints = false
profileImageView.widthAnchor.constraint(equalToConstant: commentImageSize).isActive = true
profileImageView.heightAnchor.constraint(equalToConstant: commentImageSize).isActive = true
profileImageView.topAnchor.constraint(equalTo: profileView.topAnchor, constant: 2).isActive = true
profileImageView.leadingAnchor.constraint(equalTo: profileView.leadingAnchor, constant: 2).isActive = true
// Time Ago Label
timeAgoLabel.translatesAutoresizingMaskIntoConstraints = false
timeAgoLabel.topAnchor.constraint(equalTo: usernameLabel.topAnchor).isActive = true
timeAgoLabel.leadingAnchor.constraint(equalTo: usernameLabel.trailingAnchor, constant: 0)
timeAgoLabel.trailingAnchor.constraint(equalTo: contentView.trailingAnchor, constant: -borderSize).isActive = true
// Username Label
usernameLabel.translatesAutoresizingMaskIntoConstraints = false
usernameLabel.leadingAnchor.constraint(equalTo: profileView.trailingAnchor, constant: borderSize).isActive = true
usernameLabel.trailingAnchor.constraint(equalTo: timeAgoLabel.leadingAnchor, constant: -borderSize).isActive = true
// *****
// (Not work)
// usernameLabel.topAnchor.constraint(equalTo: profileView.topAnchor).isActive = true
// (Work)
usernameLabel.topAnchor.constraint(equalTo: contentView.topAnchor, constant: borderSize).isActive = true
// *****
// Comment Label
commentLabel.translatesAutoresizingMaskIntoConstraints = false
commentLabel.leadingAnchor.constraint(equalTo: usernameLabel.leadingAnchor).isActive = true
commentLabel.trailingAnchor.constraint(equalTo: contentView.trailingAnchor, constant: -borderSize).isActive = true
commentLabel.topAnchor.constraint(equalTo: usernameLabel.bottomAnchor, constant: 2).isActive = true
commentLabel.bottomAnchor.constraint(lessThanOrEqualTo: contentView.bottomAnchor, constant: -borderSize).isActive = true
在实现我的目标之前,我尝试了另一个实现。具体的实现是以下几点:
usernameLabel.topAnchor.constraint(equalTo: profileView.topAnchor).isActive = true
后,我用下面的代码,一切完美地工作:
usernameLabel.topAnchor.constraint(equalTo: contentView.topAnchor, constant: borderSize).isActive = true
码的两条线应该有我的期望相同的效果。我想知道为什么我原来的暗示不能按预期工作。
谢谢。
答
我想知道为什么我的原始implmentation不起作用 预计。
此代码
usernameLabel.topAnchor.constraint(equalTo: profileView.topAnchor).isActive = true
意味着你正在调整您的usernameLabel
的顶部的profileView
顶部,我相信你也知道。
而这种代码:
usernameLabel.topAnchor.constraint(equalTo: contentView.topAnchor, constant: borderSize).isActive = true
意味着你把约束到细胞或细胞专门的contentView
。
现在,你需要使多行UILabel
正确地适应一个单元格是给它一个限制其顶视图的顶部和底部。这就像使用UIScrollView
一样,你需要对双方都有适当的约束。
再次,在你的第一个代码中,你基本上缺少一个重要的约束,它是对superview顶端的约束。您可以根据需要在Storyboard/Interface Builder上实验。
因为我对'profileView'约束已经超过其顶层的顶部,如果我将'usernameLabel'顶部对齐到'profileView'的顶部,它不应该暗示有' usernameLabel'顶部,为多行标签提供顶部限制? – Wilfred
你是对的,如果所有的垂直约束条件都是连接的,并且暗示他们推动了超视图的顶部和底部,那么多行标签应该正确放置在那里。但是,使用'lessThanOrEqualTo'或'greaterThanOrEqualTo'可能无济于事,只需用'equalTo'来替换它。 – Glenn