Swift-UILabel文本无法正确显示

问题描述 投票:0回答:1

我不知道为什么我的UILabel不能正确显示,但是Swift像下面的图片一样将其缩短了:

enter image description here

这是我创建label和两个lines的方法:

let oderLabel: UILabel = {
    let v = UILabel()
    v.translatesAutoresizingMaskIntoConstraints = false
    v.font = UIFont(name: "AvenirNext-DemiBold", size: 15)
    v.textColor = .white
    v.textAlignment = .center
    v.text = "ODER"
    return v
}()

let lineLeft: UIImageView = {
    let v = UIImageView()
    v.translatesAutoresizingMaskIntoConstraints = false
    v.image = UIImage(named: "line")
    return v
}()

let lineRight: UIImageView = {
    let v = UIImageView()
    v.translatesAutoresizingMaskIntoConstraints = false
    v.image = UIImage(named: "line")
    return v
}()

还有我的constraints

    oderLabel.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
    oderLabel.bottomAnchor.constraint(equalTo: weiterButton.bottomAnchor, constant: 40).isActive = true

    lineLeft.centerYAnchor.constraint(equalTo: oderLabel.centerYAnchor).isActive = true
    lineLeft.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 30).isActive = true
    lineLeft.trailingAnchor.constraint(equalTo: oderLabel.leadingAnchor).isActive = true

    lineRight.centerYAnchor.constraint(equalTo: oderLabel.centerYAnchor).isActive = true
    lineRight.leadingAnchor.constraint(equalTo: oderLabel.trailingAnchor).isActive = true
    lineRight.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: -30).isActive = true

我只想将label居中,并在lines旁边都留一点点空间。而且应该在所有iPhone尺寸上正确显示。我现在在这上的时间太长了。

应该是1分钟的任务,所以我可能会有一些误会。如果有人可以在这里帮助我,我将非常感激:)

我不知道为什么我的UILabel无法正确显示,但是Swift缩短了它,就像您在下图中看到的那样:这是我创建标签和两行代码的方式:let ...

ios swift constraints uilabel
1个回答
0
投票
向oderLabel添加具有适当大小的宽度锚点可以达到目的。
© www.soinside.com 2019 - 2024. All rights reserved.