For loop Swift 3-创建循环按钮

For loop Swift 3-创建循环按钮,for-loop,swift3,uibutton,For Loop,Swift3,Uibutton,大家好,我在for循环中创建了14个按钮,但在一个变量中每个都有7个按钮,例如: for i in 0...6 { var button = UIButton() ... var button2 = UIButton() ... } let buttonWidth = self.view.frame.size.width / 7 var xAxis : CGFloat = 1 let yAxis : CGFloat = self.view.frame.size.

大家好,我在for循环中创建了14个按钮,但在一个变量中每个都有7个按钮,例如:

for i in 0...6 {
    var button = UIButton()
    ...
    var button2 = UIButton()
    ...
}
let buttonWidth = self.view.frame.size.width / 7
var xAxis : CGFloat = 1
let yAxis : CGFloat = self.view.frame.size.height - (tileWidth * 2) - 100
let yAxis2 : CGFloat = self.view.frame.size.height - (tileWidth + 100)
for i in 0...13 {

    var button = UIButton()
    ...

}
但我想在一个变量中创建14个按钮,如图所示:

for i in 0...6 {
    var button = UIButton()
    ...
    var button2 = UIButton()
    ...
}
let buttonWidth = self.view.frame.size.width / 7
var xAxis : CGFloat = 1
let yAxis : CGFloat = self.view.frame.size.height - (tileWidth * 2) - 100
let yAxis2 : CGFloat = self.view.frame.size.height - (tileWidth + 100)
for i in 0...13 {

    var button = UIButton()
    ...

}

在图片中,我在一个for循环中使用了两个按钮

我的尺码,xAxis和yAxis必须是:

for i in 0...6 {
    var button = UIButton()
    ...
    var button2 = UIButton()
    ...
}
let buttonWidth = self.view.frame.size.width / 7
var xAxis : CGFloat = 1
let yAxis : CGFloat = self.view.frame.size.height - (tileWidth * 2) - 100
let yAxis2 : CGFloat = self.view.frame.size.height - (tileWidth + 100)
for i in 0...13 {

    var button = UIButton()
    ...

}
那么如何使用相同大小和轴的for循环创建14个按钮呢?像这样:

for i in 0...6 {
    var button = UIButton()
    ...
    var button2 = UIButton()
    ...
}
let buttonWidth = self.view.frame.size.width / 7
var xAxis : CGFloat = 1
let yAxis : CGFloat = self.view.frame.size.height - (tileWidth * 2) - 100
let yAxis2 : CGFloat = self.view.frame.size.height - (tileWidth + 100)
for i in 0...13 {

    var button = UIButton()
    ...

}
编辑

    let emptySpace:CGFloat = 40 //how much you want (left + right)
    var xAxis:CGFloat = emptySpace/2
    let space:CGFloat = 2
    let buttonWidth = ((self.view.frame.size.width)-((space*7)+emptySpace)) / 7

    var yAxis : CGFloat = self.view.frame.size.height - (tileWidth * 2) - 100
    let yAxis2 : CGFloat = self.view.frame.size.height - (tileWidth + 100)
    for i in 1...14 {

        let button = UIButton(type: .roundedRect)
        button.frame = CGRect(x: xAxis, y: yAxis, width: buttonWidth, height: buttonWidth)
        button.layer.cornerRadius = 10
        xAxis = xAxis+buttonWidth+space
        button.backgroundColor = UIColor.orange
        view.addSubview(button)


        if i%7 == 0 {
            xAxis = emptySpace/2
            yAxis = yAxis2+space
        }

    }

谢谢你,兄弟,但我能得到右屏幕和左屏幕的空间吗@KarthikeyanBosecheck编辑的一个。但给出10、20或40这样的值并不适合所有设备。这几乎就像harcode试图动态地赋予价值一样谢谢你,你太棒了!:)@卡提克扬博斯