[英]How to replace a UILabel with a UIView programmatically
我有一個嵌套在 StackView 內部的 UILabel,我需要創建一個 UIView,它將在屏幕上占據與 UILabel 完全相同的位置,然后隱藏 UILabel(這是應用程序的高級部分) ,所以如果它是免費版本,我需要用鎖圖標替換數據)。 我下面的代碼創建了視圖,但它的位置遠在標簽所在位置的右下角。 我怎樣才能完成我想要做的事情?
let testView = UIView()
testView.frame = CGRect(x: hrrLabel.frame.origin.x, y: hrrLabel.frame.origin.y, width: hrrLabel.frame.width, height: hrrLabel.frame.height)
testView.backgroundColor = UIColor.red
hrrLabel.isHidden = true
hrrLabel.addSubview(testView)
只需將testView
放在hrrLabel
的邊界中。 (假設您想放在 TOP 上的視圖是testView
—— 不完全確定應該是哪個視圖)。
let testView = UIView()
testView.frame = hrrLabel.bounds //Bounds here, not frame
testView.backgroundColor = UIColor.red
hrrLabel.isHidden = true
hrrLabel.addSubview(testView)
//Hide view without hiding subviews
hrrLabel.backgroundColor = hrrLabel.backgroundColor.withAlphaComponent(alpha: 0)
//hrrLabel.isHidden = true
查找CGRect.bounds
和CGRect.frame
之間的區別,它將使創建子視圖更容易。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.