如何让UISegmentControl中最后一个被触摸的段在Swift中突出显示?

时间:2016-06-21 01:22:41

标签: xcode swift uisegmentedcontrol

我有这个UISegmentControl有三个段,我按下其中一个并突出显示。然后我按下一个按钮来隐藏UISegmentControl,当我回到UISegmentControl时,我按下的最后一个段不再突出显示。我该如何解决这个问题?

func pressSettingsButton() {

    var customSC = UISegmentedControl()
    let items = ["blue", "orange", "red"]

    customSC = UISegmentedControl(items: items)
    customSC.selectedSegmentIndex = 0
    customSC.frame = (CGRectMake(self.view!.bounds.width/2 - 540/2, 65 * scaleFactor, 250, 30))

    customSC.layer.cornerRadius = 5.0
    customSC.backgroundColor = UIColor.blackColor()
    customSC.tintColor = UIColor.whiteColor()
    customSC.addTarget(self, action: #selector(GameScene.changeColor(_:)), forControlEvents: .ValueChanged)
    self.view!.addSubview(customSC)
 }

      if node.name == "settings" {
        //shows UISegmentControl

        pressSettingsButton()
    }


    if node.name == "closesettings" {
        //close segment control

        customSC.hidden = true 
    }

1 个答案:

答案 0 :(得分:0)

看起来每次调用pressSettingsButton时都将分段控件设置为0:

customSC.selectedSegmentIndex = 0

您可以使用您存储在游戏中的此值的设置(即pressSettingsButton(colorIndex))调用它,然后将分段控件设置为该值,如下所示:

customSC.selectedSegmentIndex = colorIndex

然而格式化会关闭,所以也许你可以提供更多细节。