How to programmatically replace UIToolBar items built in IB

后端 未结 8 886
我在风中等你
我在风中等你 2020-12-13 21:35

I have a toolbar with various image buttons, created in Interface Builder.

I\'d like to be able to programmatically replace one of the buttons with an activity indic

8条回答
  •  遥遥无期
    2020-12-13 22:23

    For the entire toolbar that you created in IB (that is to say, not the individual toolbar items), create an IBOutlet:

    @IBOutlet weak var toolbarThatYouMade: UIToolbar!
    

    This is an array of individual toolbar items, so you would access the leftmost member (for example) with a [0] index:

    toolbarThatYouMade.items![0].image = UIImage(named: "New Image")
    

    This code assumes that you have an image named "New Image" in your assets.

    You can then trigger an image change for a toolbar item without having to access that specific item itself; for example, if you were using this for something like a media player, you could toggle pause/play images from:
    a) the Pause/Play button itself,
    b) the Stop button,
    c) when you are notified of a change of player state,
    or
    d) something else entirely. (Be brave! Be bold! Be something else that begins with 'b'!)

提交回复
热议问题