Adding buttons to toolbar programmatically in swift

Hobbyist

I'm having a hard time adding a button to the toolbar in swift, below you can see an image of the toolbar that I'm after, unfortunately even though I have it designed in my Storyboard file, it doesn't show up when setting the toolbar to be visible.

The way that I have designed this is two items, the first being a flexable space element, and the second being an add element. It looks like this:

enter image description here

Here's the code that I've used to attempt to replicate this in code:

self.navigationController?.toolbarHidden = false
self.navigationController?.toolbarItems = [UIBarButtonItem]()
self.navigationController?.toolbarItems?.append(
    UIBarButtonItem(barButtonSystemItem: .FlexibleSpace, target: self, action: nil)
)
self.navigationController?.toolbarItems?.append(
    UIBarButtonItem(barButtonSystemItem: .Add, target: self, action: "onClickedToolbeltButton:")
)

As you can see I'm setting the toolbar to be visible, initializing (and clearing) the toolbarItems array of UIBarButtonItem, and then adding two UIBarButtonItem's to the array, in the proper order.

However, the toolbelt remains empty, why is this?

vadian

The usual way to do that is to create the array of toolbar items and then assign the array to the items property of the toolbar.

self.navigationController?.isToolbarHidden = false
var items = [UIBarButtonItem]()
items.append(
    UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: nil, action: nil)
)
items.append(
    UIBarButtonItem(barButtonSystemItem: .add, target: self, action: #selector(onClickedToolbeltButton(_:)))
)
toolbarItems = items

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Android Adding Buttons to Toolbar Programmatically

Buttons Programmatically on Swift 4

Adding onClick to buttons that are created programmatically

Calling API and adding buttons programmatically

Android: programmatically adding buttons to notification

Swift: No reaction from toolbar buttons

Adding UIButton to toolbar swift 5

Programmatically adding a grid of buttons inside Relative layout

Android: Adding multiple buttons programmatically bug

shift bar buttons to left side in toolbar with swift

programmatically toolbar items not showing iOS swift 4

Swift Spritekit Adding Button programmatically

Adding UIGestureRecognizer To Subview Programmatically in Swift

iOS adding constraints programmatically in Swift

Adding custom buttons to Kendo grid toolbar in ASP.NET Core

ONSEN-UI Adding more than three buttons on the toolbar

Programmatically add buttons to array before viewDidLoad in swift

Swift - Adding labels to stack views programmatically

Adding and changing a custom UIView programmatically (Swift)

Adding a UIView to a UITableViewController with NSLayoutConstraints programmatically with Swift 4

Adding a UIControl into title of Navbar Programmatically - Swift

Issue with adding constraints to UIImage programmatically | Swift

Make 2 toolbar/footer buttons with 50% width with Swift/Storyboard

Nav bar with toolbar buttons on a specific tabview item in swift

adding popover to programmatically added UIBarButtonItem and adding images to the same popover Swift

how do I rotate a group of buttons programmatically in Swift?

iOS Swift - How to assign a default action to all buttons programmatically

All the toolbar buttons of Froala

Excel Toolbar buttons