如果给定初始值,我试图从循环中生成多组分段控制按钮。
所以如果值是3 我需要循环在彼此之下生成3组分段按钮
这是我在尝试调整教程中的代码失败后到目前为止所拥有的。
var numberOfVillains = ["1", "2", "3", "4"]
var buttonY: CGFloat = 20 // our Starting Offset, could be 0
for number in numberOfVillains {
let segmentController = UISegmentedControl()
//let villainButton = UISegmentedControl(frame: CGRect(x: 50, y: buttonY, width: 50, height: 30)){
buttonY = buttonY + 50 // we are going to space these UIButtons 50px apart
segmentController.frame = CGRect(x:100, y:200, width: 200,height: 30)
//segment frame size
segmentController.insertSegment(withTitle: "1", at: 0, animated: true)
//inserting new segment at index 0
segmentController.insertSegment(withTitle: "2", at: 1, animated: true)
//inserting new segment at index 1
segmentController.backgroundColor = UIColor.blue
//setting the background color of the segment controller
segmentController.selectedSegmentIndex = 0
//setting the segment which is initially selected
segmentController.addTarget(self, action: "segment:", for: UIControlEvents.valueChanged)
//calling the selector method
self.view.addSubview(segmentController)
//adding the view as subview of the segment comntroller w.r.t. main view controller
}
答案 0 :(得分:1)
每个分段控制器都有相同的帧,因此它们相互叠加。您必须使用buttonY
:
segmentController.frame = CGRect(x:100, y:buttonY, width: 200,height: 30)
我认为你会看到差异