作者热门文章
- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
我试图将两个 UILabel
放在一行中:一个 UILable
在左侧,文本左对齐,一个 UILabel
在右侧,文本右对齐。请看上图。它们有不同的字体大小。
我使用了以下自动布局约束:
let labelHorizontalConstrains = NSLayoutConstraint.constraints(
withVisualFormat: "H:|-20-[nameLabel]-15-[birthDeathDateLabel]-20-|",
metrics: nil,
views: views)
allConstraints += labelHorizontalConstrains
但结果显示它们不是垂直对齐的:左边的UILabel
低于右边的UILabel
。如何解决这个问题?
最佳答案
您需要添加一个centerYAnchor
约束。
birthDeathDateLabel.centerYAnchor.constraint(equalTo: nameLabel.centerYAnchor).isActive = true
但是,如果您希望它与文本的底部对齐,您应该使用 UIStackView
。
let stackView = UIStackView(arrangedSubviews: [nameLabel, birthDeathDateLabel])
stackView.alignment = .firstBaseline
stackView.axis = .horizontal
// Add other stackview properties and constraints
关于ios - 如何在一行内垂直对齐两个 UILabel?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54385298/
我是一名优秀的程序员,十分优秀!