【问题标题】:Toggle between select/deselect UIButton在选择/取消选择 UIButton 之间切换
【发布时间】:2016-09-22 09:34:07
【问题描述】:

我有一个按钮,我想在点击(选择)时更改背景颜色和文本,并在再次点击(取消选择)时将其恢复到原始状态。我可以选择它,但我无法取消选择它。我对 SO 进行了研究,但在 if-else 部分的代码中出现错误

   @IBAction func btn1Pressed(_ sender: AnyObject) {
        sender.setTitleColor(UIColor.blue, for: UIControlState.normal)
        (sender as! UIButton).backgroundColor = UIColor.green

        if sender.isSelected {
            sender.selected = false
        }
        else {
            sender.selected = true
        }
    }

【问题讨论】:

  • 试试改成(sender as! UIButton).selected = !(sender as! UIButton).selected
  • 它没有恢复到原来的状态,但是有一个蓝色的高亮
  • 您的默认背景色广告文字是什么?在您的代码中,您只需将按钮属性更改为您选择的状态,而不是恢复
  • 默认背景颜色为蓝色,文字为绿色

标签: ios swift uibutton


【解决方案1】:

试试这个:

@IBAction func btnPressed(_ sender: AnyObject) {
    guard let button = sender as? UIButton else { return }

    if !button.isSelected {
        button.isSelected = true
        button.setTitleColor(UIColor.blue, for: UIControl.State.normal)
        button.backgroundColor = UIColor.green
    }
    else {
        button.isSelected = false
        button.setTitleColor(UIColor.green, for: UIControl.State.normal)
        button.backgroundColor = UIColor.blue
    }
}

【讨论】:

  • 选择/取消选择都有效,唯一的问题是当我选择按钮时,字体上有一个蓝色突出显示
  • 在故事板中将按钮的 tintColor 更改为 clearColor :D
  • 效果很好:)。如果你不介意,再问一个问题,我有十个这样的按钮,所以我需要为所有这十个按钮编写动作方法
  • 尝试搜索插座组,然后您可以制作一个按钮数组,然后在 viewDidLoad 中创建一个循环并为它们添加相同的操作,非常快
【解决方案2】:

在我看来 UISwitch 就是您要找的东西。尝试一下,而不是浪费时间来实现已经为您服务的东西。

【讨论】:

    【解决方案3】:

    试试这个

     @IBAction func btn1Pressed(sender: UIButton) {
    
    
        if sender.selected {
            sender.selected = false
            sender.setTitleColor(UIColor.redColor(), forState: .Normal)
             sender.backgroundColor = UIColor.blackColor()
    
    
        }
        else {
            sender.selected = true
            sender.setTitleColor(UIColor.redColor(), forState: .Selected)
            sender.backgroundColor = UIColor.blackColor()
        }
    }
    

    【讨论】:

    • 我之前试过这个,我得到了这些错误“类型“布尔”被破坏”和“无法分配给属性:'sender'是一个'让'常量”
    【解决方案4】:

    storyboard 更改所选按钮的背景颜色,如下图state config 选择selected,然后更改背景颜色并自定义普通按钮的默认值。

    并在下面使用

    (sender as! UIButton).selected = !(sender as! UIButton).selected
    

    【讨论】:

    • 我收到错误消息“无法分配给属性:'sender' 是 'let' 常量”
    • 检查我更新的答案,从故事板自定义选择和默认按钮配置,并在他的操作中切换按钮状态。
    【解决方案5】:

    请先尝试此代码,您需要创建按钮的操作和属性,例如

    @IBOutlet var btn_ButtonPressed: UIButton!
    

    然后行动。

        @IBAction func click_ButtonPressed(_ sender: Any) {
      if !sender.isSelected() {
        sender.selected = true
        btn_ButtonPressed.setTitleColor(UIColor.red, forState: .normal)
        btn_ButtonPressed.backgroundColor = UIColor.yellow
        }
    else {
        sender.selected = false
        btn_ButtonPressed.setTitleColor(UIColor.yellow, forState: .normal)
        btn_ButtonPressed.backgroundColor = UIColor.red
        }
    }
    

    【讨论】:

    • 我尝试了完全相同的方法,但我得到了这些错误“类型“布尔”已损坏”和“无法分配给属性:'sender' 是一个'让'常量”
    • @Coder221 你用它的出路来改变 isSelected 属性。
    【解决方案6】:

    这个对我来说很好用! // @IBAction func buttonColorChanger(sender : UIButton ) {

    if button.isSelected == false
    {
    
        button.backgroundColor = UIColor.purple
        print("selected")
    
        button.setTitle("selected", for: .normal)
        button.setTitleColor(UIColor.white, for: .normal)
         button.isSelected = true
    }else{
    
        button.backgroundColor = UIColor.white
        print("unselected")
        button.isSelected = false
    
    }
           }
    

    【讨论】:

      【解决方案7】:

      更简洁的方法是:

      @objc private func buttonTapped(_ sender: UIButton) {
          sender.isSelected.toggle()
      
          if selected {
              // Setup appearance for selected state.
          } else {
              // Setup appearance for deselected state.
          }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-08-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-10-31
        • 2014-12-05
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多