【问题标题】:UIButton not changing image unless there's a background color除非有背景颜色,否则 UIButton 不会更改图像
【发布时间】:2021-01-05 10:50:41
【问题描述】:

这很奇怪,我尝试了 UIControl.State() 的不同排列。除非我为图像添加背景颜色,否则实际图像不会改变。

func followUserLocation {
    let button = UIButton(type: UIButton.ButtonType.custom)
    button.frame = CGRect(x: 10, y: 500, width: 50, height: 50)
    
    if followUserStatus {
      followUserButton = UIImage(named: "follow_user_high")
      button.setImage(followUserButton, for: .normal)
      let coordinateRegion = MKCoordinateRegion(center: coordinatesToAppend, latitudinalMeters: 2500, longitudinalMeters: 2500)
      vcTrainMapView.setRegion(coordinateRegion, animated: true)
      print("0 AA - TRUE \(followUserButton) \(UIControl.State())")
    } else {
      followUserButton = (UIImage(named: "follow_user"))
      button.setImage(followUserButton, for:.selected)
      print("0 BB- FALSE \(followUserButton) \(UIControl.State())")
    }

    button.addTarget(self, action: #selector(buttonAction), for: .touchUpInside)

//    button.backgroundColor = .lightGray // Once I utilise this, it works.
    self.view.addSubview(button)
  }

这是打印出来的。 UIControl.State() 在按下时也不会改变。

0 AA - TRUE Optional(<UIImage:0x60000154b0f0 named(main: follow_user_high) {40, 40}>) UIControlState(rawValue: 0)
0 AA - TRUE Optional(<UIImage:0x60000154b0f0 named(main: follow_user_high) {40, 40}>) UIControlState(rawValue: 0)
0 BB- FALSE Optional(<UIImage:0x600001504a20 named(main: follow_user) {40, 40}>) UIControlState(rawValue: 0)
0 BB- FALSE Optional(<UIImage:0x600001504a20 named(main: follow_user) {40, 40}>) UIControlState(rawValue: 0)
0 AA - TRUE Optional(<UIImage:0x60000154b0f0 named(main: follow_user_high) {40, 40}>) UIControlState(rawValue: 0)
0 AA - TRUE Optional(<UIImage:0x60000154b0f0 named(main: follow_user_high) {40, 40}>) UIControlState(rawValue: 0)
0 AA - TRUE Optional(<UIImage:0x60000154b0f0 named(main: follow_user_high) {40, 40}>) UIControlState(rawValue: 0)

【问题讨论】:

    标签: swift uibutton


    【解决方案1】:

    我认为您应该删除 if 语句,然后将按钮图像设置为 .normal.selected 状态。之后,更新按钮选择状态。

    func followUserLocation {
        let button = UIButton(type: UIButton.ButtonType.custom)
        button.frame = CGRect(x: 10, y: 500, width: 50, height: 50)
    
        followUserButton = UIImage(named: "follow_user_high")
        button.setImage(followUserButton, for:.selected)
    
        followUserButton = (UIImage(named: "follow_user"))
        button.setImage(followUserButton, for: .normal)
    
        if followUserStatus {
          let coordinateRegion = MKCoordinateRegion(center: coordinatesToAppend, latitudinalMeters: 2500, longitudinalMeters: 2500)
          vcTrainMapView.setRegion(coordinateRegion, animated: true)
        }
    
        button.addTarget(self, action: #selector(buttonAction(_:)), for: .touchUpInside)
        self.view.addSubview(button)
      }
    
    @objc private func buttonAction(_ sender: UIButton) {
        sender.isSelected.toggle()
        print(sender.isSelected)
    }
    

    【讨论】:

    • 它不工作。我确实在按下按钮之前和之后添加了一些打印输出,它仍然显示图像被命名为“关注用户”。当我交换代码使得“follow_user_high”是第二个代码序列时,它将适用于第一次按下(打印输出仍然显示没有改变 - 但将显示“follow_user_high”,因为这是最新的代码序列)。再次按下时,按钮不再变化。
    • 你是如何添加代码进行测试的?我不确定它是如何打印的,但您可以在按下按钮时看到图像发生了变化。
    • 我复制并粘贴了您的代码。图像没有改变。看这个屏幕录像dropbox.com/s/klr2k6b16fck98q/dvvva.mov?dl=0
    猜你喜欢
    • 1970-01-01
    • 2014-04-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-22
    • 2021-11-04
    • 2018-12-17
    相关资源
    最近更新 更多