簡體   English   中英

UISegmentedControl的程序約束不起作用

[英]Programatic constraints with UISegmentedControl not working

我正在嘗試使用UIKit編程約束來創建一個簡單的界面,在該界面的左側是一個帶有UISegmentedControl的行,右側是一個黃色的UIView。

問題是我希望UIView可以擴展到整個視圖高度,但是由於某種原因,它的底部錨點不在段控制器的實際底部。

在此處輸入圖片說明

這是代碼

parentGuide = top.layoutMarginsGuide

let choices = UISegmentedControl(items: ["Op1", "Op2"])
choices.translatesAutoresizingMaskIntoConstraints = false
top.addSubview(choices)
choices.leadingAnchor.constraint(equalTo: parentGuide.leadingAnchor).isActive = true
choices.topAnchor.constraint(equalTo: parentGuide.topAnchor).isActive = true

let yellow = UIView()
yellow.translatesAutoresizingMaskIntoConstraints = false
top.addSubview(yellow)
yellow.backgroundColor = UIColor.yellow
yellow.trailingAnchor.constraint(equalTo: parentGuide.trailingAnchor).isActive = true
yellow.topAnchor.constraint(equalTo: parentGuide.topAnchor).isActive = true
yellow.leadingAnchor.constraint(equalTo: parentGuide.trailingAnchor, constant: -40).isActive = true
yellow.bottomAnchor.constraint(equalTo: choices.layoutMarginsGuide.bottomAnchor).isActive = true

為什么會發生這種情況,我該如何解決?

代替

yellow.bottomAnchor.constraint(equalTo: choices.layoutMarginsGuide.bottomAnchor).isActive = true

做了

yellow.bottomAnchor.constraint(equalTo: choices.bottomAnchor).isActive = true

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM