【问题标题】:Button title does not show up按钮标题不显示
【发布时间】:2020-04-05 11:38:44
【问题描述】:

我得到了一个带有背景图像和标题的 UIButton。 Wenn 它被按下(并按住),我应该改变它的背景图像(它可以工作),但保留它的标题。但是当我按下按钮时,标题消失了,即使释放,它也不会再次购物。

    @IBAction func holdingTheButton(_ sender: Any) {

            numberButton.setImage(UIImage(named: "buttonIn.png"), for: .normal)
            numberButton.setTitle("Refuel", for: .normal)
}

这是我的 holdingTheButton 函数的一部分(已经声明/初始化按钮等),我尝试保留标题“加油”。顺便说一句:更改为“for:.highlighted”或“for:.selected”不会改变任何内容。

Screenshot of my Buttons configuration

【问题讨论】:

    标签: swift xcode button uibutton


    【解决方案1】:

    当您按下 UIButton 时,它的状态会在单击时变为突出显示。 UIButton 有不同的状态,例如选中、正常、突出显示等等。

    如果您希望图像和标题保持不变,您必须再次调用“setTitle”和“setImage”以获得“突出显示”状态

    在您的“ViewDidLoad”中执行以下操作:

      numberButton.setImage(UIImage(named: "buttonIn.png"), for: .normal)
      numberButton.setTitle("Refuel", for: .normal)
            numberButton.setImage(UIImage(named: "buttonIn.png"), for: .highlighted)
      numberButton.setTitle("Refuel", for: .highlighted)
    

    【讨论】:

    • 谢谢!但是按钮标题仍然没有出现
    • 尝试对选定状态“.selected”执行相同操作,并在“viewDidLoad”函数中而不是在“holdingTheButton”函数中执行此操作
    • 哦,我明白了。我使用的是 .setImage 而不是 .setBackgroundImage
    猜你喜欢
    • 2023-03-15
    • 2015-02-18
    • 2017-12-19
    • 2019-08-26
    • 1970-01-01
    • 1970-01-01
    • 2014-12-03
    • 2017-12-31
    • 1970-01-01
    相关资源
    最近更新 更多