Why setting the barItems is not working? - ios

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

Related

Unable to change backBarButtonItem in Swift

I want to make my backBarButtonItem to be only < , without back. I have searched stackOverflow and found some solutions, but it stays < back.
I tried this in my pushed VC:
navigationItem.backBarButtonItem = UIBarButtonItem(title: "", style: .plain, target: nil, action: nil)
and also this, in my AppDelegate:
navigationController.navigationItem.backBarButtonItem = UIBarButtonItem(title: "", style: .plain, target: nil, action: nil)
But none of two helped me. Anyone knows why?
This is how I push my ViewControllers:
let VC = XYZViewController()
self.navigationController?.pushViewController(VC, animated: true)
navigationItem.backBarButtonItem = UIBarButtonItem(title: "", style: .plain, target: nil, action: nil)
Write this piece of code in the controller from which you are pushing to the next controller

How can I add title and image on a bar buttonitem for a toolbar of navigation controller

How it is possible to add an image and text on a UIBarButtonItem on a navigation controller just like on modern apps such as Photos, ebay etc (see Toolbar)
I know this is possible in Interface Builder if you add a toolbar manually to a view but this is not possible if using navigation controller.
If you have a UIViewController in a UINavigationController you can set the left and the right bar button of the view controller with
// System item
self.navigationItem.leftBarButtonItem = UIBarButtonItem(barButtonSystemItem: .action, target: self, action: #selector(action))
self.navigationItem.rightBarButtonItem = UIBarButtonItem(barButtonSystemItem: .action, target: self, action: #selector(action))
// Tittle
self.navigationItem.leftBarButtonItem = UIBarButtonItem(title: "Left Button", style: .plain, target: self, action: #selector(action))
self.navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Right Button", style: .plain, target: self, action: #selector(action))
// Image
self.navigationItem.leftBarButtonItem = UIBarButtonItem(image: image, style: .plain, target: self, action: #selector(action))
self.navigationItem.rightBarButtonItem = UIBarButtonItem(image: image, style: .plain, target: self, action: #selector(action))
// Custom view
self.navigationItem.leftBarButtonItem = UIBarButtonItem(customView: view)
self.navigationItem.rightBarButtonItem = UIBarButtonItem(customView: view)
This is as far as I got so far. This, however only displays a text right to the image:
//Create a UIButton with an image on the left, and text to the right
var buttonImage = #imageLiteral(resourceName: "Lab")
var button : UIButton = UIButton(type: UIButtonType.custom)
button.setImage(buttonImage, for: UIControlState.normal);
button.setTitle("Caption", for: UIControlState.normal);

My add button in my iOS 10 app is not performing an action

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)

Hide back button in tabbar navigation controller

I need to hide the Back that is overlapping with Cart
Edit 1
I have already added these things
override func viewWillAppear(animated: Bool) {
super.viewDidAppear(animated)
self.tabBarController!.navigationItem.title = "Orders"
// self.navigationItem.rightBarButtonItem = UIBarButtonItem(title:"Cart", style: UIBarButtonItemStyle.Plain, target: self, action: nil)
self.tabBarController!.navigationItem.rightBarButtonItem = UIBarButtonItem(title:"Cart", style: UIBarButtonItemStyle.Plain, target: self, action: nil)
self.tabBarController!.navigationItem.leftBarButtonItem = UIBarButtonItem(title:"Cart", style: UIBarButtonItemStyle.Plain, target: self, action: nil)
self.navigationItem.hidesBackButton = true
}
override func viewDidLoad() {
super.viewDidLoad()
self.tabBarController!.navigationItem.title = "Orders"
self.navigationItem.title = "Order History"
// self.navigationItem.rightBarButtonItem = UIBarButtonItem(title:"Cart", style: UIBarButtonItemStyle.Plain, target: self, action: nil)
self.tabBarController!.navigationItem.rightBarButtonItem = UIBarButtonItem(title:"Cart", style: UIBarButtonItemStyle.Plain, target: self, action: nil)
self.tabBarController!.navigationItem.leftBarButtonItem = UIBarButtonItem(title:"Cart", style: UIBarButtonItemStyle.Plain, target: self, action: nil)
// Do any additional setup after loading the view.
self.navigationItem.hidesBackButton = true
}
in this place
self.navigationItem.hidesBackButton = true
try this
self.navigationItem.leftBarButtonItem = UIBarButtonItem(customView: UIView())
option-2
self.tabbarcontroller.navigationcontroller.navigationitem.hidesBackButton = true
self.tabBarController!.navigationItem.rightBarButtonItem = UIBarButtonItem(title:"Cart", style: UIBarButtonItemStyle.Plain, target: self, action: nil)
self.tabBarController!.navigationItem.leftBarButtonItem = UIBarButtonItem(title:"Cart", style: UIBarButtonItemStyle.Plain, target: self, action: nil)
self.navigationItem.hidesBackButton = YES;
Try with below code, write this code on view controller which appear after 1st navigationcontroller ( PushViewController ).
self.navigationItem.setHidesBackButton(true, animated: false)
self.tabBarController!.navigationItem.hidesBackButton = true

Adding buttons to toolbar programmatically in swift

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)

Resources