![](/img/trans.png)
[英]How to set UILabel only width and height and constraints programmatically
[英]What the extra width and height constraints appears at programmatically creating UILabel? And how can I prevent theirs creating?
我以編程方式在我的應用程序中創建 UI 元素,例如:
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
let label = UILabel()
label.text = "Just text"
label.backgroundColor = #colorLiteral(red: 0.721568644, green: 0.8862745166, blue: 0.5921568871, alpha: 1)
view.addSubview(label)
label.translatesAutoresizingMaskIntoConstraints = false
label.leftAnchor.constraint(equalTo: view.leftAnchor, constant: 20).isActive = true
label.topAnchor.constraint(equalTo: view.topAnchor, constant: 20).isActive = true
view.rightAnchor.constraint(greaterThanOrEqualTo: label.rightAnchor, constant: 20).isActive = true
view.layoutIfNeeded()
}
UILabel 創建並正常顯示,但除了來自我的代碼標簽的 3 個約束之外,還有寬度和高度約束
調試視圖層次截圖:
iOS模擬器中的應用截圖:
調試控制台上的標簽大小約束信息:
po ((UIView *)0x7f8573c0ccc0).constraints
<__NSArrayI 0x600000236820>(
<NSContentSizeLayoutConstraint:0x6000000a8b20 UILabel:0x7f8573c0ccc0'Just text'.width == 66 Hug:250 CompressionResistance:750 (active)>,
<NSContentSizeLayoutConstraint:0x6000000a8b80 UILabel:0x7f8573c0ccc0'Just text'.height == 20.3333 Hug:250 CompressionResistance:750 (active)>
)
這里有自動創建的寬度和高度約束,沒有我的代碼。 我想阻止他們的創造。
label 和 superview 之間的約束:
po ((UIView *)((UIView *)0x7f8573c0ccc0).superview).constraints
<__NSArrayI 0x60000028c3f0>(
<NSLayoutConstraint:0x600000284a60 H:|-(20)-[UILabel:0x7f8573c0ccc0'Just text'](LTR) (active, names: '|':UIView:0x7f8573c0b7b0 )>,
<NSLayoutConstraint:0x6000002854b0 V:|-(20)-[UILabel:0x7f8573c0ccc0'Just text'] (active, names: '|':UIView:0x7f8573c0b7b0 )>,
<NSLayoutConstraint:0x600000285550 H:[UILabel:0x7f8573c0ccc0'Just text']-(>=20)-|(LTR) (active, names: '|':UIView:0x7f8573c0b7b0 )>,
<NSLayoutConstraint:0x600000285cd0 'UIView-Encapsulated-Layout-Height' UIView:0x7f8573c0b7b0.height == 736 (active)>,
<NSAutoresizingMaskLayoutConstraint:0x600000285d70 h=-&- v=-&- 'UIView-Encapsulated-Layout-Left' UIView:0x7f8573c0b7b0.minX == 0 (active, names: '|':UIWindow:0x7f8573d132d0 )>,
<NSAutoresizingMaskLayoutConstraint:0x600000285eb0 h=-&- v=-&- 'UIView-Encapsulated-Layout-Top' UIView:0x7f8573c0b7b0.minY == 0 (active, names: '|':UIWindow:0x7f8573d132d0 )>,
<NSLayoutConstraint:0x600000285c80 'UIView-Encapsulated-Layout-Width' UIView:0x7f8573c0b7b0.width == 414 (active)>
)
在這里,我們可以查看由我的代碼創建的 UILabel 的 3 個約束和容器視圖和 UIWindows 的系統約束
為什么會出現寬高約束?
我可以阻止他們的創作嗎?
這些約束被創建,因為 UILabel 有一個內在的內容 size 。 您可以使用 UILabel 上的內容擁抱 (CHP) 和內容壓縮阻力 (CCRP) 優先級來影響這些約束。
它們將始終被創建,但如果您添加優先級高於 CHP / CCRP 的約束,則您可以在布局中“覆蓋”它們。
您可以以編程方式添加高度和寬度約束。 但是您應該忽略(而不是添加)高度和寬度約束,如果您希望自動布局約束根據內容大小自動處理標簽的高度和寬度。
高度約束:
label.addConstraint(NSLayoutConstraint(item: label, attribute: NSLayoutAttribute.height, relatedBy: NSLayoutRelation.equal, toItem: nil, attribute: NSLayoutAttribute.notAnAttribute, multiplier: 1, constant: 20.0))
寬度約束:
label.addConstraint(NSLayoutConstraint(item: label, attribute: NSLayoutAttribute.width, relatedBy: NSLayoutRelation.equal, toItem: nil, attribute: NSLayoutAttribute.notAnAttribute, multiplier: 1, constant: 20.0))
寬度約束對於右錨意味着更少,您已在代碼中添加。 您應該添加寬度約束或右錨。
view.rightAnchor.constraint(greaterThanOrEqualTo: label.rightAnchor, constant: 20).isActive = true
寬度約束將固定標簽的寬度,因此右錨將變得無效。
我建議忽略高度和寬度約束(不要添加)。 您在此處顯示的代碼(以及界面設計)適用於 UIElement 的動態大小。 (如果你想添加任何其他 UIElement,你可以添加底部約束,相對於你當前的標簽定位。)
更新:(對於您的查詢-“我想知道為什么會出現我的代碼中遺漏的約束以及如何防止它。” )
我試過你的代碼,它工作正常。 (您可以在代碼中看到什么問題?)
let label = UILabel()
label.text = "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s. When an unknown printer took a galley of type and scrambled it n book."
label.numberOfLines = 0
label.lineBreakMode = .byTruncatingTail
label.backgroundColor = UIColor.orange
view.addSubview(label)
label.translatesAutoresizingMaskIntoConstraints = false
label.leftAnchor.constraint(equalTo: view.leftAnchor, constant: 120).isActive = true
label.topAnchor.constraint(equalTo: view.topAnchor, constant: 120).isActive = true
view.rightAnchor.constraint(greaterThanOrEqualTo: label.rightAnchor, constant: 20).isActive = true
view.layoutIfNeeded()
這是結果:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.