[英]Adding SubViews Programatically Below Each Other
我想以編程方式添加一組UITextFields。 UITextField的數量不知道,它將在For循環中創建。 我可以執行此操作,但是問題是哪個UILabel創建在另一個之上。 我該如何放置一些相互約束的約束。
目前我的代碼如下:
For Loop...
{
let Description = UILabel(frame: CGRectMake(20, 100, 300, 40))
Description.font = UIFont.systemFontOfSize(18)
...
self.view.addSubview(Description)
// StackView.addSubview(Description)
// I TRIED ADDING A STACK VIEW BUT HAD THE SAME EFFECT.
}
試試這個
var y = 100
For Loop...
{
let Description = UILabel(frame: CGRectMake(20, y, 300, 40))
y = Description.Frame.size.height
Description.font = UIFont.systemFontOfSize(18)
...
self.view.addSubview(Description)
// StackView.addSubview(Description) // I TRIED ADDING A STACK VIEW BUT HAD THE SAME EFFECT.
}
創建CGRect時必須增加Y值。
將Y值存儲在一個int變量中,並在每次迭代時將其遞增到您的buttonHeight + desiredMargin。
希望這可以幫助。
喜歡
第1步
最初創建Y
坐標並根據需要設置高度
var y : CGFloat = 3
第2步
for var index1:Int = 0 ; index1 < yourArray.count ; index1++
{
let timerLbl:UILabel = UILabel(frame: CGRectMake(5, y, self.view.frame.size.width, 35)) //UILabel(frame: CGRectMake(50, y, self.view.frame.size.width, 35))
timerLbl.textAlignment = .Center
timerLbl.textColor = UIColor.clearColor()
timerLbl.text = "00:00"
timerLbl.tag = index1
self.view.addSubview(timerLbl)
// continue the other lables
// the following line increment your height
y = y + 10
}
我不在家里,但是您應該使用類似以下的代碼:
var yRect : CGFloat = 100
let padding : CGFloat = 20
For Loop...
{
let Description = UILabel(frame: CGRectMake(20, yRect, 300, 40))
Description.font = UIFont.systemFontOfSize(18)
yRect += 40
...
self.view.addSubview(Description)
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.