Dav*_*amp 2 uilabel ios swift uistackview
我是 iOS 开发的新手,我对如何实现这一点有点困惑。
我UILabel添加了2个UIStackView像这样的:
let horizontalStackView1 = UIStackView(arrangedSubviews: [self.label1, self.label2])
Run Code Online (Sandbox Code Playgroud)
当我运行应用程序时,它看起来像这样:
香港专业教育学院尝试设置horizontalStackView1.distribution,horizontalStackView1.alignment等等,没有运气。
我如何实现这一目标?
提前致谢!
更新:
代码看起来像这样(顺便说一下,它是表格的一个单元格):
class ItemTableViewCell: UITableViewCell
{
...
let stateLabel = UILabel()
let effectiveDateLabel = UILabel()
...
var typeImage: UIImage?
{
...
}
var summary: String?
{
...
}
var effectiveDate: Date?
{
...
}
override init(style: UITableViewCellStyle, reuseIdentifier: String?)
{
super.init(style: style, reuseIdentifier: reuseIdentifier)
self.accessoryType = .disclosureIndicator
...
let horizontalStackView1 = UIStackView(arrangedSubviews: [self.stateLabel, self.effectiveDateLabel])
let horizontalStackView2 = UIStackView(arrangedSubviews: [typeImageViewWrapper, self.titleLabel])
horizontalStackView2.spacing = 4
let verticalStackView = UIStackView(arrangedSubviews: [horizontalStackView1, horizontalStackView2, self.summaryLabel])
verticalStackView.axis = .vertical
verticalStackView.spacing = 4
self.contentView.addSubview(verticalStackView)
...
}
required init?(coder aDecoder: NSCoder)
{
fatalError()
}
}
Run Code Online (Sandbox Code Playgroud)
这是因为 UIStackView 选择第一个具有最低内容拥抱优先级的排列子视图并调整其大小,以便堆栈视图的内容占据全宽。
如果您想在这种情况下使用 UIStackView,您可以更改内容拥抱优先级,例如。
label2.setContentHuggingPriority(.defaultLow, for: .horizontal)
label1.setContentHuggingPriority(.defaultHigh, for: .horizontal)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2426 次 |
| 最近记录: |