Search code examples
iosswiftuibarbuttonitemuitoolbar

Customize Bar button Item in Swift


I am very new to coding and I have been watching a numerous youtube videos on how to customize (adding my own image) to the bar button item on a toolbar and none of the ways seem to be working.

Could someone please help or point me to the right direction?

Or should I just create a button instead and add the image and "command" to that?

Thank you!

Screenshot of the problem


Solution

  • How to add an image to a UIBarButtonItem:

    // Create toolbar
    let toolbar = UIToolbar()
    toolbar.sizeToFit()
    
    // My button with an image
    let button = UIBarButtonItem(image: UIImage(named: "myImageName"), style: .done, target: self, action: nil) // Add a selector if you want an action when it is clicked
    
    // Create toolbar using items
    toolbar.items = [button]