如何突出显示栏按钮项? (迅速)

时间:2016-03-06 08:57:15

标签: ios xcode swift

我使用工具栏项目来使用动作属性,我希望当我点击项目时,图像会变为高亮显示的图像。

这是我的全部三个按钮:

@IBAction func audio(sender: AnyObject) {
    self.scView.contentOffset = CGPointMake(0,0);
}

@IBAction func mainBtn(sender: AnyObject) {
    self.scView.contentOffset = CGPointMake(415,0);
}

@IBAction func videoBtn(sender: AnyObject) {
    self.scView.contentOffset = CGPointMake(830,0);
}

@IBOutlet var inpt: UITextField!
@IBOutlet var scView: UIScrollView!

这是我的应用图片: image

感谢

1 个答案:

答案 0 :(得分:1)

如果您只想在按下按钮时显示带有突出显示图像的按钮 - 您可以通过故事板进行操作 - 您可以为default状态中的每个按钮设置图像,也可以在highlighted - 但是当您松开按钮时,它会返回到原始图像。

如果您希望在选择按钮时切换图像,则必须为按钮添加插座和按钮状态变量(如果它只更改一次,并且永远不会回到原始状态,那么你不需要国家)

以下是您可能会如何操作的示例 - 更新以重置其他'按钮

@IBOutlet weak var mainButton: UIButton!
@IBOutlet weak var videoButton: UIButton!

var mainButtonSelected : Bool = false
var videoButtonSelected : Bool = false

@IBAction func mainButton(sender: AnyObject)
{
    mainButtonSelected = !mainButtonSelected // toggle state
    if mainButtonSelected
    {
        // highlight the mainButton
        mainButton.setImage(UIImage(named: "imageMainHighlight.png"), forState: .Normal)

        // clear the highlight (if any) on videoButton
        videoButton.setImage(UIImage(named: "imageVideoDefault.png"), forState: .Normal)

        // UPDATED
        // make sure the videoButtonSelected flag is correct
        videoButtonSelected = false
        // UPDATED
    }
    else
    {
        // clear the highlight on mainButton, no need to do anything with videoButton
        mainButton.setImage(UIImage(named: "imageMainDefault.png"), forState: .Normal)
    }

    // add any other code you need here
}

@IBAction func videoButton(sender: AnyObject)
{
    videoButtonSelected = !videoButtonSelected // toggle state
    if videoButtonSelected
    {
        // highlight videoButton
        videoButton.setImage(UIImage(named: "imageVideoHighlight.png"), forState: .Normal)
        // clear the highlight (if any) on mainButton
        mainButton.setImage(UIImage(named: "imageMainDefault.png"), forState: .Normal)

        // UPDATED
        // make sure the mainButtonSelected flag is correct
        mainButtonSelected = false
        // UPDATED
    }
    else
    {
        // clear the highlight on videoButton, no need to do anything for mainButton
        videoButton.setImage(UIImage(named: "imageVideoDefault.png"), forState: .Normal)
    }

    // add any other code you need here
}