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:
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?
None of the above worked for me, but:
Swift 3 / Swift 4
self.navigationController?.isToolbarHidden = false
var items = [UIBarButtonItem]()
items.append( UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: self, action: nil) )
items.append( UIBarButtonItem(barButtonSystemItem: .add, target: self, action: #selector(add)) ) // replace add with your function
self.toolbarItems = items // this made the difference. setting the items to the controller, not the navigationcontroller
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
self.navigationController?.toolbarItems = items
self.navigationController?.setToolbarItems(items, animated: false)
self.navigationController?.toolbar.setItems(items, animated: false)
Try it.
self.navigationController?.toolbarHidden = false
var items = [UIBarButtonItem]()
items.append(
UIBarButtonItem(barButtonSystemItem: .FlexibleSpace, target: self, action: nil)
)
items.append(
UIBarButtonItem(barButtonSystemItem: .Add, target: self, action: "onClickedToolbeltButton:")
)
self.navigationController?.toolbar.setItems(items, animated: false)
Here is an example with MKUserTrackingBarButtonItem:
navigationController?.toolbarHidden = false
let barButtonItem = MKUserTrackingBarButtonItem(mapView: self.mapView)
self.toolbarItems = [barButtonItem]
Updated answer using the current selector syntax for
Swift 3
var barButtons = [UIBarButtonItem]()
barButtons.append(
UIBarButtonItem(barButtonSystemItem: .done, target: self, action: #selector(ThisViewController.onDoneBarButtonClick))
)
self.navigationItem.setRightBarButtonItems(barButtons, animated: false)
You can place this code in any loading event. It works seamless for me in viewDidLoad().
Replace "ThisViewController.onDoneBarButtonClick" with your view controller class name and any method you want to write to manage the toolbar button click.
let addButton = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.Add, target: self, action: "addSomething:")
toolbarItems = [UIBarButtonItem(barButtonSystemItem: .FlexibleSpace, target: self, action: nil),addButton]
self.navigationController!.setToolbarHidden(false, animated: false)
Related
Found this code to insert the "Done" Button within the decimal keyboard pad and it works as long I don't use a custom view for the button like in this piece of code:
extension UITextField {
func makeKeyboardToolBar(title: String) {
let keyboardToolBar = UIToolbar()
keyboardToolBar.sizeToFit()
let flexibleSpace = UIBarButtonItem(barButtonSystemItem:
UIBarButtonItem.SystemItem.flexibleSpace, target: nil, action: nil)
let bimage = UIImageView(image: UIImage(named: "icon_plus_50"))
let doneButton = UIBarButtonItem(title: title, style: UIBarButtonItem.Style.done, target: self, action: #selector(self.doneClicked))
doneButton.customView = bimage
keyboardToolBar.setItems([flexibleSpace, doneButton], animated: true)
self.inputAccessoryView = keyboardToolBar
}
#objc func doneClicked() {
self.endEditing(true)
}
}
The image appears, but doesn't react. Do not set a custom view works instead, the "title" appears and doneClicked response as appropriate.
There are similar questions but unfort. objective-c...
Any help appreciate.
Don't create or use the UIImageView. Just create the UIBarButtonItem with the image.
let doneButton = UIBarButtonItem(image: UIImage(named: "icon_plus_50"), style: .plain, target: self, action: #selector(doneClicked))
No need to set the customView.
I have two buttons inside of a toolbar that is positioned at the bottom of the screen. I would like to center those buttons within the toolbar. Everything I have found is focused on centering buttons within a nav bar and I am unsure of how to approach this issue. What am I missing?
This is the UI I am attempting to emulate:
Adding a flexible space to the left and right of the buttons centered the buttons inside of the toolbar. Thanks to Midhun MP for the tip.
Thks #Andrew, it works
but for someone who wants it from code here is the example in Swift 5.0
override func viewDidLoad() {
super.viewDidLoad()
navigationController?.setToolbarHidden(false, animated: true)
let spaceItemLeft = UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: self, action: nil)
let nextItem = UIBarButtonItem(title: "NEXT", style: .plain, target: self, action: #selector(nextTapped))
let spaceItemRight = UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: self, action: nil)
toolbarItems = [spaceItemLeft, nextItem, spaceItemRight]
}
#objc func nextTapped() {
print("NEXT Tapped")
}
I started to learn Swift 3 and Xcode a week ago.
At this moment I have an small app with a navigation bar and an add button on the right, I want it to perform something but I can't figure it out...
I have this:
func criarPessoa() {
_ = pessoas.append("Ola OLA")
}
let adicionarButao = UIBarButtonItem(barButtonSystemItem: .add, target: self, action: #selector(criarPessoa()(sender:)))
What am I doing wrong?
let adicionarButao = UIBarButtonItem(barButtonSystemItem: .add, target: self, action: #selector(criarPessoa()(sender:)))
should be
let adicionarButao = UIBarButtonItem(barButtonSystemItem: .add, target: self, action: #selector(NameOfTheVCTheFunctionIsIn.criarPessoa()))
You also need to add the BarButton like so :
self.navigationItem.setLeftBarButtonItems([adicionarButao], animated: false)
or:
self.navigationItem.setRightBarButtonItems([adicionarButao], animated: false)
I have a UIViewController, and I embed it into a UINavigationController.
I want to show one item in the toolbar (and by toolbar, I mean this:
This is my code in viewDidLoad method
self.navigationController?.toolbarHidden = false
self.navigationController?.toolbar.items?.append(UIBarButtonItem(title: "Buy Potato", style: .Plain, target: self, action: #selector(ViewController.buyPotato)))
self.navigationController?.toolbarItems?.append(UIBarButtonItem(title: "Buy Potato", style: .Plain, target: self, action: #selector(ViewController.buyPotato)))
self.toolbarItems?.append(UIBarButtonItem(title: "Buy Potato", style: .Plain, target: self, action: #selector(ViewController.buyPotato)))
and I already have the method buyPotato
func buyPotato() {
}
as you see, I tried to do that using either the viewController or the navigationController, but it doesn't work.
All I can see is the toolbar at the bottom of my screen but without any button.
self.navigationController?.toolbarHidden = false
var items = [UIBarButtonItem]()
items.append(
UIBarButtonItem(barButtonSystemItem: .Plain, target: self, action: nil))
items.append(
UIBarButtonItem(barButtonSystemItem: .Add, target: self, action: "onClickedToolbeltButton:"))
self.setToolbarItems(barButtonItems, animated: true)
This has to work for you as per the answer written here.
Delete
self.setToolbarItems(barButtonItems, animated: true)
Add
self.toolbarItems = barButtonItems
In an iOS app I am making, I have set the identifier of a bar button item programmatically using:
homeTitleBar.leftBarButtonItem = UIBarButtonItem(barButtonSystemItem: .Trash, target: self, action: nil)
Which changes the button to a trash can.
My question is, after that, how can I then change it to a custom image.
homeTitleBar.leftBarButtonItem?.image = UIImage(named: "settingsIcon")
does not seem to be working.
Try
homeTitleBar.leftBarButtonItem = UIBarButtonItem(image:UIImage(named: "settingsIcon"), style: UIBarButtonItemStyle.Plain, target: self, action: nil)