【问题标题】:How to change the icon of a Bar Button when pressed in Swift?在 Swift 中按下时如何更改条形按钮的图标?
【发布时间】:2018-06-24 02:00:41
【问题描述】:

我正在 Swift 中创建秒表,并且我想在按下按钮以启动秒表时将我为条形按钮选择的播放图标更改为暂停图标。你是怎么做到的?

【问题讨论】:

    标签: ios swift uibarbuttonitem


    【解决方案1】:

    您不能在运行时更改UIBarButtonItem 的样式。您必须删除UIBarButtonItem,然后添加您想要的UIBarButtonItem

    @IBOutlet weak var toolBar: UIToolbar!
    var pauseButton = UIBarButtonItem()
    var playButton = UIBarButtonItem()
    var arrayOfButtons = [AnyObject]()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        pauseButton = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.Pause, target: self, action: "pauseButtonTapped")
        playButton = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.Play, target: self, action: "playButtonTapped")
    
        arrayOfButtons = self.toolBar.items!
        arrayOfButtons.insert(playButton, atIndex: 0) // change index to wherever you'd like the button
        self.toolBar.setItems(arrayOfButtons, animated: false)
    }
    
    func playButtonTapped() {
        arrayOfButtons = self.toolBar.items!
        arrayOfButtons.removeAtIndex(0) // change index to correspond to where your button is
        arrayOfButtons.insert(pauseButton, atIndex: 0)
        self.toolBar.setItems(arrayOfButtons, animated: false)
    }
    
    func pauseButtonTapped() {
        arrayOfButtons = self.toolBar.items!
        arrayOfButtons.removeAtIndex(0) // change index to correspond to where your button is
        arrayOfButtons.insert(playButton, atIndex: 0)
        self.toolBar.setItems(arrayOfButtons, animated: false)
    }
    

    UIBarButtonItem Class Reference

    【讨论】:

      【解决方案2】:

      对于 Swift 3

      这就是我在 Swift 3 中的做法:

      var favoritesBarButtonOn: UIBarButtonItem!
      var favoritesBarButtonOFF: UIBarButtonItem!
      
      favoritesBarButtonOn = UIBarButtonItem(image: #imageLiteral(resourceName: "TabBarIconSettingsOff"), style: .plain, target: self, action: #selector(didTapFavoritesBarButtonOn))
      favoritesBarButtonOFF = UIBarButtonItem(image: #imageLiteral(resourceName: "TabBarIconSettingsOn"), style: .plain, target: self, action: #selector(didTapFavoritesBarButtonOFF))
      
      self.navigationItem.rightBarButtonItems = [self.rightNavBarButton, self.favoritesBarButtonOn]
      
      func didTapFavoritesBarButtonOn() {
          self.navigationItem.setRightBarButtonItems([self.rightNavBarButton, self.favoritesBarButtonOFF], animated: false)
          print("Show Favorites")
      }
      
      func didTapFavoritesBarButtonOFF() {
          self.navigationItem.setRightBarButtonItems([self.rightNavBarButton, self.favoritesBarButtonOn], animated: false)
          print("Show All Chat Rooms")
      }
      

      对于 Swift 4

      var favoritesBarButtonOn: UIBarButtonItem! var favoritesBarButtonOFF: UIBarButtonItem!

      favoritesBarButtonOn = UIBarButtonItem(image: #imageLiteral(resourceName: "TabBarIconSettingsOff"), style: .plain, target: self, action: #selector(didTapFavoritesBarButtonOn))
      favoritesBarButtonOFF = UIBarButtonItem(image: #imageLiteral(resourceName: "TabBarIconSettingsOn"), style: .plain, target: self, action: #selector(didTapFavoritesBarButtonOFF))
      
      self.navigationItem.rightBarButtonItems = [self.favoritesBarButtonOn]
      
      func didTapFavoritesBarButtonOn() {
          self.navigationItem.setRightBarButtonItems([self.favoritesBarButtonOFF], animated: false)
          print("Show Favorites")
      }
      
      func didTapFavoritesBarButtonOFF() {
          self.navigationItem.setRightBarButtonItems([self.favoritesBarButtonOn], animated: false)
          print("Show All Chat Rooms")
      }
      

      【讨论】:

        【解决方案3】:

        我相信您已经为您的问题找到了解决方案,但我会留下它以防有人仍然需要它。

        UIBarButtonItem 不是UIControl,但是您可以使用自定义视图初始化它,即自定义UIButton,如下所示:

        let playButton = UIButton(frame: CGRectMake(0, 0, 30, 30))
        playButton.addTarget(self, action: "togglePlay:", forControlEvents: .TouchUpInside)
        playButton.setImage(UIImage(named: "play-active"), forState: .Normal)
        playButton.setImage(UIImage(named: "play-inactive"), forState: .Selected)
        let rightButton = UIBarButtonItem(customView: playButton)
        self.navigationItem.setRightBarButtonItems([rightButton], animated: true)
        

        来自Custom "Pressed" UIBarButtonItem Backgrounds的想法

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2020-06-14
          • 2021-03-25
          • 2023-02-21
          • 2015-07-12
          • 2016-01-30
          相关资源
          最近更新 更多