2015-11-05 57 views
0

我想以编程方式添加一组UITextFields。 UITextFields的数量不知道,它将在For循环中创建。我可以执行此操作,但问题是创建UILabels放在彼此之上。我怎样才能放置一些放在彼此下面的约束条件。以编程方式将SubViews添加到彼此之下

目前我的代码如下:

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. 
} 

回答

1

你必须在创建的CGRect递增Y值。

将Y值存储在int变量中,并在每次迭代时将其增加到buttonHeight + desiredMargin。

希望这会有所帮助。

+0

这是执行此的最佳方法是什么?它不会影响不同的屏幕尺寸? –

+0

您将使用scrollView的权利?只是不使用一个常数值的宽度 – Nanoc

1

不喜欢

第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 
        } 
+0

如果我想字段的宽度来填充视图,而不是一个静态值我该怎么做? –

+0

如果你想填充宽度使用** self.view.frame.size.width **,如果任何新的设备在未来,它会自动填充宽度,检查更新的答案,对不起,迟交回复 –

+0

好吗谢谢很多 –

2

试试这个

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. 


     } 
1

我不在家里,但你应该使用的代码类似的东西:

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) 
} 
相关问题