【问题标题】:Swift: Change a button color once pressedSwift:按下后更改按钮颜色
【发布时间】:2015-10-01 02:04:56
【问题描述】:

我正在尝试为我的 iPhone 制作一个基本应用程序。我对 Xcode 完全陌生,我在 google 上浏览过,但找不到我要找的东西。我正在尝试为我的飞机制作一份清单。

我基本上有一个带有几个按钮的视图控制器。我想要的是按钮一旦按下就会从灰色变为蓝色并保持蓝色,除非再次按下。我对 Xcode 和 Swift 语言一无所知,所以如果有人可以提供帮助,请像在向孩子解释一样解释。

到目前为止,我已经成功完成所有页面的设置和滚动工作,但我现在只需要更改按钮。

提前谢谢你, 泰。

【问题讨论】:

    标签: ios iphone swift button


    【解决方案1】:

    这很简单。

    您将首先在 ViewDidLoad() 方法中实际设置按钮的背景颜色。在此之前,您应该已经为按钮设置了一个 IBOutlet。在我的示例中,我只是将插座命名为通用“按钮”。

    您的 IBOutlet 将是:

    @IBOutlet weak var button:UIButton!
    

    别忘了将插座也连接到按钮上!

    接下来,在 ViewDidLoad 中将按钮的背景图片设置为灰色,如下所示:

    override func viewDidLoad() {
        super.viewDidLoad()
    
        button.backgroundColor = UIColor.grayColor()
    }
    

    这应该给你的按钮一个通用的灰色背景。如果您想更好地控制实际颜色,可以将button.backgroundColor = UIColor.grayColor() 替换为button.backgroundColor = UIColor(red: CGFloat, green: CGFloat, blue: CGFloat, alpha: CGFloat),将“CGFloat”替换为您选择的值。

    接下来是改变按钮颜色的实际代码:

    制作一个 IBAction 并将其连接到按钮。我的叫:

    @IBAction func buttonTapped(sender: AnyObject) {}
    

    您的条件是,如果按钮为灰色,点击时应变为蓝色,如果为蓝色,则点击时应变为灰色。

    因此,在我们刚刚定义的 IBAction 中,您将添加一个 If 语句,如下所示:

    @IBAction func buttonTapped(sender: AnyObject) {
        if button.backgroundColor == UIColor.grayColor() {
            button.backgroundColor = UIColor.blueColor()
        }
        else if button.backgroundColor == UIColor.blueColor() {
            button.backgroundColor = UIColor.grayColor()
        }
    }
    

    这个 if 语句的作用是,如果按钮的背景颜色为灰色,则将其设置为蓝色,或者如果背景颜色为蓝色,则将其设置为灰色。您可以根据需要将UIColor.BlueColor()UIColor.GrayColor() 替换为更个性化的UIColor(red: CGFloat, green: CGFloat, blue: CGFloat, alpha: CGFloat)

    编辑:如果您只想更改按钮的文本颜色,只需在所需位置添加此语句或其变体即可:

    button.setTitleColor(UIColor.grayColor(), forState: UIControlState.Selected)
    

    【讨论】:

      【解决方案2】:

      使用UIButton类的这个方法

      func setTitleColor(_ color: UIColor?,
                forState state: UIControlState)
      

      您要查找的状态是 .Selected

      当然,您必须自己处理选定状态,因为切换按钮不是本机行为。

      【讨论】:

      • 当用户将手指放在按钮上时,状态将变为 .highlighted,例如:button.setTitleColor(UIColor.white.withAlphaComponent(0.5), for: .highlighted)
      【解决方案3】:

      对于 Swift 3:

      // for change background color of a selected button in a collection view
      func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
          let collectionCell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath)
          collectionCell.button.addTarget(self, action: #selector(button_Select(_:)), for: .touchUpInside) // add target for action
      
          return collectionCell
      }
      
      @objc func button_Select(_ sender: UIButton) {
          if sender.isSelected == true {
              sender.backgroundColor = UIColor.uicolorFromHex(0x262D35, alpha: 100)
              sender.setTitleColor(UIColor.white, for: .normal)
              sender.isSelected = false
          } else {
              sender.backgroundColor = UIColor.uicolorFromHex(0xF3CD70, alpha: 100)//Choose your colour here
              sender.setTitleColor(UIColor.white, for: .normal) //To change button Title colour .. check your button Tint color is clear_color..
              sender.isSelected = true
          }
      }
      

      没有 CollectionView:

       //MARK: - UIButton Action
        @IBAction func colorChangingBtn(_ sender: UIButton){
            sender.isSelected = !sender.isSelected
            if sender.isSelected{
              sender.backgroundColor = UIColor.gray
              sender.setTitleColor(UIColor.blue, for: .normal)
               }
            else{
              sender.backgroundColor = UIColor.blue
              sender.setTitleColor(UIColor.white, for: .normal)
             }
         }
      

      【讨论】:

        【解决方案4】:

        更新 SWIFT 5

        记得在选择状态时也要调用颜色

        @IBAction func yourBtnAction(_ sender: UIButton) {
        
            sender.isSelected = !sender.isSelected
            sender.tintColor = .clear
        
            if sender.isSelected{
                sender.setTitleColor(.green, for: .selected)
               }
            else{
                sender.setTitleColor(.red, for: .selected)
             }
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2020-04-20
          • 2019-06-28
          • 1970-01-01
          • 1970-01-01
          • 2013-07-16
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多