Adding UIBarButtonSystemItem icons to Nav-Toolbar (SWIFT) - swift

I have been trying to find a way to add the UIBarButtonSystemItem icon to the toolbar. I have implemented it on my NavBar with the following code:
self.navigationItem.leftBarButtonItem = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.Action, target: self, action: "shareMeme")
But when it comes to the toolbar, I am at a loss. Any ideas?

Try:
var items: Array<UIBarButtonItem> = []
items.append(UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.Action, target: self, action: "selector"))
self.setToolbarItems(items as [AnyObject], animated: true)
self.navigationController?.setToolbarHidden(false, animated: true)

Related

dismiss keyboard after input

I am trying to build my first iOS app with swift 5.
I have a textfield where the user should type a value like 15,00
this is my result at the moment:
I searched for a solution to dismiss the keyboard.
Option 1: dismiss keyboard after user type a format like xx,xx
Option 2: a "Done" button im keyboard with decimal pad style
But I don't know, how I can realize any of this option.
Need help ! :)
You can add this code to your didLoad
var toolbar = UIToolbar()
toolbar.sizeToFit()
var flexibleSpace = UIBarButtonItem(barButtonSystemItem: UIBarButtonItem.SystemItem.flexibleSpace, target: nil, action: nil)
var doneButton = UIBarButtonItem(barButtonSystemItem: UIBarButtonItem.SystemItem.done, target: self, action: #selector(yourFunc))
toolbar.setItems([flexibleSpace, doneButton], animated: true)
yourTextField.inputAccessoryView = toolbar
When click done button you can do whatever you want but we want to do dismiss keyboard
#objc func doneClicked(){
searchBar.endEditing(true)
}

How to center UITabButtonItem in the middle of the screen programatically?

I am trying to move the DONE button in the middle.
Initially, it is located on the left side:
let doneButton = UIBarButtonItem(title: "Done", style: .done, target: self, action: #selector(ViewController.dismissKeyboard))
toolBar.setItems([doneButton], animated: false)
toolBar.isUserInteractionEnabled = true
Do you have an idea about how to move it to the middle of the screen programmatically? I tried to add flexible space but it doesn't work.
You must two create UIBarButtonItem with SystemItem: .flexibleSpace
and add to toolbar items first and last item :
toolBar.setItems([UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: nil, action: nil), doneButton, UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: nil, action: nil)], animated: false)

How to change navigation header button text in xcode project

I'm adding a button to my nav view bar like this:
let navItem = UINavigationItem(title: "Waiting Room");
let doneItem = UIBarButtonItem(barButtonSystemItem: UIBarButtonItem.SystemItem.done, target: nil, action: #selector(addTapped));
navItem.leftBarButtonItem = doneItem
mainNavBar.setItems([navItem], animated: false);
The title of the view bar changes to Waiting room when I do this,
but how do I change the text of the button itself that I'm adding
from "Done" to something else?
I'm using Swift 4.0
Try below code:
let doneItem = UIBarButtonItem(title: "Your title", style: .plain, target: self, action:#selector(addTapped));
navigationItem.leftBarButtonItem = doneItem
Hope this will help you :)

NavigationItem right button not setting

I am losing my mind setting the right button on my custom Navigation Controller.
I have my class that extends the UINavigationCOntroller. I cleaned everything and am just runnig the following code on the viewDidAppear.
navigationBar.topItem?.title = "Carpool"
let languagesSpinner = UIBarButtonItem(title: "PT", style: .done, target: self, action: #selector(didClickLanguagesButton))
navigationItem.setRightBarButton(languagesSpinner, animated: true)
The title is seted up but not the button.
The strange thing is that, the item is setted up when i debug with po.
Can anyone help me please?
You are setting title with navigationBar.topItem and setting rightBarButton with navigationItem so try setting rightBarButton also with navigationBar.topItem .
Try once
navigationBar.topItem?.setRightBarButton(languagesSpinner, animated: true)
Instead of
navigationItem.setRightBarButton(languagesSpinner, animated: true)

UIBarButtonItems created programatically with Swift, not visible

I'm not sure why this piece of code which is supposed to embed two bar button items in a navigation controller's toolbar won't work. The toolbar itself is visible when I run my code, but not the bar button items. What am I doing wrong here? Thanks for attention.
class NavigationController: UINavigationController {
override func viewDidLoad() {
super.viewDidLoad()
//Tool bar appearance
toolbar.barTintColor = UIColor.blackColor()
//Show tool bar by default
self.navigationController?.toolbarHidden = false
//Icons all located in images.xcassets
let homeImage = UIImage(named: "home")
let gameImage = UIImage(named: "logo")
var toolBarItems = [UIBarButtonItem]()
let homeButton = UIBarButtonItem(image: homeImage, style: UIBarButtonItemStyle.Plain, target: self, action: #selector(NavigationController.toHome))
homeButton.title = "Home"
let gameButton = UIBarButtonItem(image: gameImage, style: UIBarButtonItemStyle.Plain, target: self, action: #selector(NavigationController.toGame))
homeButton.title = "Game"
//Place the bar items in toolBarItems array
toolBarItems.append(homeButton)
toolBarItems.append(gameButton)
//self.toolbar.items = toolBarItems
self.toolbar.setItems(toolBarItems, animated: true)
}//End viewDidLoad
func toHome() {
let homeVC = HomeViewController(nibName: "HomeViewController", bundle: nil)
self.pushViewController(homeVC, animated: true)
}
func toGame() {
let gameVC = GameViewController(nibName: "GameViewController", bundle: nil)
self.pushViewController(gameVC, animated: true)
}
}
Did you create a second .swift file for your dependent controller? You should move this code to the dependant controller file
self.navigationController?.toolbarHidden = false
let button1 = UIBarButtonItem(title: "home", style: .Plain, target: self, action: #selector(SecondViewController.home))
let myToolBar = [button1]
self.setToolbarItems(myToolBar, animated: true)
I am not sure but I think your buttons has size 0. So maybe you should add some constraints or view frame size. You can try debugging using the view hierarchy debugger.
Maybe you just have to replace this self.navigationController?.toolbarHidden = false
With this toolbarHidden = false
I'm confused by your code. Is the class you show us, the one Navigation Controller where all other ViewController depend on or is it itself one dependent ViewController that in this case appears to be a Navigation Controller? Or is it a Navigation Controller by mistake? (not very likely)
Why I ask? At one time you're referring to the parent navigation controller with self.navigationController?.toolbarHidden = false which is not this navigation controller itself. Then in the rest of the code, you refer to this controller itself.
Hope this leads to the right thinking.