如何在 swift 中动态创建堆栈视图?
How to create stack views dynamical in swift?
您好,我为四个堆栈视图开发了一个程序,用于在同一视图中添加另一个堆栈,我需要另一个子视图,有没有其他方法可以使用 for 循环添加子视图,我是 swift 语言的新手和学习,需要帮助
如何将所有堆栈视图保留在 for 循环中以便我可以添加 n 个堆栈视图
这是使用 UIStackViews 构建键盘的代码片段...
func newPad() {
keyPadWindow = UIView(frame: CGRect(x: 128, y: 224, width: 512, height: 356))
keyPadWindow.backgroundColor = UIColor.yellowColor()
var keyCount = 0
keyPadSV = UIStackView(frame: CGRect(x: 0, y: 0, width: 512, height: 356))
keyPadSV!.axis = .Vertical
keyPadSV!.spacing = 0.0
keyPadSV!.alignment = .Center
keyPadSV!.distribution = .EqualSpacing
keyPadSV!.backgroundColor = UIColor.redColor()
for var k2s = 0; k2s < 5; k2s++ {
let keyPadSVB = UIStackView(frame:CGRect(x: 0, y: 0, width: 0, height: 0))
keyPadSVB.axis = .Horizontal
keyPadSVB.spacing = 0.0
keyPadSVB.alignment = .Center
keyPadSVB.distribution = .EqualSpacing
keyPadSVB.backgroundColor = UIColor.greenColor()
for var k4s = 0; k4s < 4; k4s++ {
let button = UIButton(type: UIButtonType.Custom) as UIButton
button.frame = CGRectMake(0, 0, 0, 0)
button.backgroundColor = UIColor.greenColor()
button.tag = keyCount
let blah = "x" + String(keyCount)
let blahIMAGE = UIImage(named: blah)
button.setImage(blahIMAGE, forState: UIControlState.Normal)
button.addTarget(self, action: "keyPadPress:", forControlEvents: UIControlEvents.TouchUpInside)
keyPadSVB.addArrangedSubview(button)
keyCount++
}
keyPadSV.addArrangedSubview(keyPadSVB)
}
keyPadWindow.addSubview(keyPadSV)
self.view.addSubview(keyPadWindow)
}
这取决于你有 16 张图像来显示键。
您好,我为四个堆栈视图开发了一个程序,用于在同一视图中添加另一个堆栈,我需要另一个子视图,有没有其他方法可以使用 for 循环添加子视图,我是 swift 语言的新手和学习,需要帮助
如何将所有堆栈视图保留在 for 循环中以便我可以添加 n 个堆栈视图
这是使用 UIStackViews 构建键盘的代码片段...
func newPad() {
keyPadWindow = UIView(frame: CGRect(x: 128, y: 224, width: 512, height: 356))
keyPadWindow.backgroundColor = UIColor.yellowColor()
var keyCount = 0
keyPadSV = UIStackView(frame: CGRect(x: 0, y: 0, width: 512, height: 356))
keyPadSV!.axis = .Vertical
keyPadSV!.spacing = 0.0
keyPadSV!.alignment = .Center
keyPadSV!.distribution = .EqualSpacing
keyPadSV!.backgroundColor = UIColor.redColor()
for var k2s = 0; k2s < 5; k2s++ {
let keyPadSVB = UIStackView(frame:CGRect(x: 0, y: 0, width: 0, height: 0))
keyPadSVB.axis = .Horizontal
keyPadSVB.spacing = 0.0
keyPadSVB.alignment = .Center
keyPadSVB.distribution = .EqualSpacing
keyPadSVB.backgroundColor = UIColor.greenColor()
for var k4s = 0; k4s < 4; k4s++ {
let button = UIButton(type: UIButtonType.Custom) as UIButton
button.frame = CGRectMake(0, 0, 0, 0)
button.backgroundColor = UIColor.greenColor()
button.tag = keyCount
let blah = "x" + String(keyCount)
let blahIMAGE = UIImage(named: blah)
button.setImage(blahIMAGE, forState: UIControlState.Normal)
button.addTarget(self, action: "keyPadPress:", forControlEvents: UIControlEvents.TouchUpInside)
keyPadSVB.addArrangedSubview(button)
keyCount++
}
keyPadSV.addArrangedSubview(keyPadSVB)
}
keyPadWindow.addSubview(keyPadSV)
self.view.addSubview(keyPadWindow)
}
这取决于你有 16 张图像来显示键。