【问题标题】:How to hide show UIButton text not button in IOS Swift [closed]如何在IOS Swift中隐藏显示UIButton文本而不是按钮[关闭]
【发布时间】:2018-09-21 03:20:52
【问题描述】:

1我在 iOS swift 中遇到了问题。我试图隐藏和显示添加到UITextview 列表中的UIButton 文本。我想在单击时隐藏按钮文本并在重新单击按钮时显示。我不能保持 nil 按钮文本,因为我根据字母表为按钮提供背景颜色。

[]

请帮帮我 提前致谢

我的问题没有解决

【问题讨论】:

  • 在这里也发布您的输入,而不仅仅是文本。
  • 请发布代码 sn-ps 以及您已实现的内容,以便人们了解您的问题并知道如何提供帮助。

标签: ios swift


【解决方案1】:
override func viewDidLoad() {
    super.viewDidLoad()
    myButton.setTitle("myTitle", for: .normal)
    myButton.setTitle("", for: .selected)
}

@IBAction func myButtonClicked(_ sender: UIButton) {
    myButton.isSelected = !myButton.isSelected
}

【讨论】:

  • 重复
【解决方案2】:

正如您所说,您不能将按钮的文本设为 nil,您应该这样做,

你也可以实现这个 Bool 扩展,

extension Bool {
    mutating func toggle() {
        self = !self
    }
}


@IBAction func myButton(_ sender: UIButton) {
    sender.titleLabel?.isHidden.toggle()
}

这将显示和隐藏您的 Button 的 titleLabel 文本。

更新

@IBAction func btnTapped(_ sender: UIButton) {
    sender.isSelected.toggle()
    if sender.isSelected == true {
        sender.setTitleColor(UIColor.clear, for: .normal)
    } else {
        sender.setTitleColor(UIColor.blue, for: .normal)
    }
}

【讨论】:

  • 不适合我
  • @Softlabsindia 您面临什么问题?
  • 我只想在点击时隐藏和显示按钮标签
  • @Softlabsindia 对您有用吗?如有任何疑问,请告诉我
【解决方案3】:
@IBAction func myButton(_ sender: UIButton) {
    if sender.currentTitle = "" {
        sender.setTitle("myTitle", for: .normal)
    } else {
        sender.setTitle("", for: .normal)
    }
}

【讨论】:

  • 不需要这个方法
【解决方案4】:
  1. 在正常状态和选中状态下设置按钮标题颜色
  2. 点击按钮时,只需简单地改变状态。

// 设置

// let button = <your button>
button.setTitleColor(<yourColor>, for: .normal)
button.setTitleColor(UIColor.clear, for: .selected)

// 动作

@IBAction func didSelectButton(_ button: UIButton) {
    button.isSelected = !button.isSelected
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-11-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-02
    • 2013-12-30
    • 1970-01-01
    • 2020-12-08
    相关资源
    最近更新 更多