简体   繁体   English

Swift 4-在AutoLayout UITableViewCell中以编程方式切碎的多行标签

[英]Swift 4 - Multi-line label chopped in AutoLayout UITableViewCell programmatically

Please find below my code to layout the UI programming of a UITableViewCell using AutoLayout: 请在下面的代码中查找使用AutoLayout布局UITableViewCell的UI程序:

// 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

This is what I would like to achieve: 这是我要实现的目标: 用户名标签贴到contentView

Before achieve my target, I tried another implementation. 在达到目标之前,我尝试了另一种实现。 The implementation is as following: 实现如下:

usernameLabel.topAnchor.constraint(equalTo: profileView.topAnchor).isActive = true

However, by unknown reason, the multiple line comment label was tripped if using this implementation. 但是,由于未知原因,如果使用此实现,则多行注释标签会跳闸。 用户名标签贴在个人资料照片上

After I use the following code, things work perfectly: 使用以下代码后,一切运行正常:

usernameLabel.topAnchor.constraint(equalTo: contentView.topAnchor, constant: borderSize).isActive = true

Both lines of codes should have the same effect in my expectation. 在我的期望中,这两行代码应具有相同的效果。 I would like to know why my original implmentation does not work as expected. 我想知道为什么我的原始实现无法按预期工作。

Thanks. 谢谢。

I would like to know why my original implmentation does not work as expected. 我想知道为什么我的原始实现无法按预期工作。

This code 这段代码

usernameLabel.topAnchor.constraint(equalTo: profileView.topAnchor).isActive = true 

means that you are aligning your usernameLabel 's top to the top of profileView , which I believe you also know. 意味着您正在将usernameLabel的顶部与profileView的顶部对齐,我相信您也知道。

And this code: 这段代码:

usernameLabel.topAnchor.constraint(equalTo: contentView.topAnchor, constant: borderSize).isActive = true

means you are putting constraint to the cell or specifically cell's contentView . 意味着您要对单元格或单元格的contentView施加约束。

Now, what you will need to make your multiline UILabel fit in a cell properly is to give it a constraint to top and bottom of its superview. 现在,您需要使多行UILabel正确地适合单元格,是对其进行限制 ,使其成为其父视图的顶部和底部 It's like using a UIScrollView , you need proper constraints to each sides. 就像使用UIScrollView ,您需要对每边进行适当的约束。

Again, in your first code, you're basically missing an important constraint which is constraint to the superview top. 同样,在您的第一个代码中,您基本上缺少了一个重要的约束,即对Superview顶部的约束。 You can experiment things on Storyboard / Interface Builder as you wish. 您可以根据需要在Storyboard / Interface Builder上进行实验。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM