gpt4 book ai didi

ios - 对象 swift 3.0 上的 NSLayoutConstraints

转载 作者:可可西里 更新时间:2023-11-01 02:05:11 25 4
gpt4 key购买 nike

目前我正在使用这行代码在左上角的 UILabel 上设置原点...

addConstraint(NSLayoutConstraint(item: messageLabel, attribute: .centerY, relatedBy: .equal, toItem: self, attribute: .centerY, multiplier: 1, constant: 0))

但是,如果有意义的话,我希望原点距离左边缘 H 和高度的一半为 0 像素。

有什么建议吗?我试过更改乘数但无济于事。

我想做的是移动原点,使 (0,0) 不是左上角,而是标签最左边和标签高度的一半,这样我就可以将标签正确居中。

最佳答案

澄清一下,您需要标签左对齐并从上到下居中?

你看过NSLayoutAnchor了吗? ?

messageLabel = UILabel(/* Initialized somehow */)
messageLabel.translatesAutoresizingMaskIntoConstraints = false

view.addSubview(messageLabel)
view.centerYAnchor.constraint(equalTo: messageLabel.centerYAnchor).isActive = true
view.leadingAnchor.constraint(equalTo: messageLabel.leadingAnchor).isActive = true

关于ios - 对象 swift 3.0 上的 NSLayoutConstraints,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43429476/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com