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

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)

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 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 :)

Changing navigationItem.rightBarButton isn't resulting in visual changes

For whatever reason my rightBarButton is 1. not going away when I set the rightBarButton to nil and 2. not changing when I set it to another UIBarButtonItem.
I can successfully change the title on the fly so I don't suspect the inherited navigation controller is the problem.
self.navigationItem.rightBarButtonItem = UIBarButtonItem(image: UIImage(named: "Settings"), style: .Plain, target: self, action: nil)
This simply does nothing and the right bar button remains as a button with text "Cancel".
Probably you are have some problem with image: UIImage(named: "Settings"). Check the name of the image or try to use this to fill a UIImageView
EDIT:
this the code that I used to fill a item with an image and act like a button
//set frame
button.frame = CGRectMake(0, 0, 30, 30)
button.addTarget(self, action: #selector(TorchView.showRank(_:)), forControlEvents: .TouchUpInside)
let barButton = UIBarButtonItem(customView: button)
let editButton = UIBarButtonItem(image: UIImage(named: "question"), style: .Plain, target: self, action: #selector(TorchView.tapQuestion))
//assign button to navigationbar
self.navigationItem.rightBarButtonItems = [editButton, barButton]
In this case I am using 2 rightButtons

Adding UIBarButtonSystemItem icons to Nav-Toolbar (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)

How to add a "Continue" button to a top Navigation Bar in iOS/Swift

(Xcode6, iOS8, iPhone, Swift)
I would like to add a "Continue" button on the right of the navigation bar.
How can this be accomplished? I've been trying with some of the methods available on UIBarButtonItem, but can't get it working.
My best effort to date has been:
var b = UIBarButtonItem(title: "Continue", style: UIBarButtonItemStyle, target: self, action: nil)
self.navigationItem.rightBarButtonItem = b
But I'm getting an error on the first line. It doesn't like the "style" parameter. I've also tried
var b = UIBarButtonItem(title: "Continue", style: UIBarButtonItemStylePlain, target: self, action: nil)
But no luck. Still stuck on the style parameter. Any ideas? tyvm Keith :D
EDIT: For posterity, the following line also includes an action setting:
var b = UIBarButtonItem(title: "Continue", style: .Plain, target: self, action:"sayHello")
Reference:
How to set the action for a UIBarButtonItem in Swift
enum UIBarButtonItemStyle : Int {
case Plain
case Bordered
case Done
}
So you should NOT write 'UIBarButtonItemStyle', but use '.Plain' (note the dot preceding the word):
var b = UIBarButtonItem(title: "Continue", style: .Plain, target: self, action: nil)
To add an actual action to the button you use the following (assuming the method sayHello() exists):
var b = UIBarButtonItem(title: "Continue", style: .Plain, target: self, action: Selector("sayHello"))
Edit: Typo in code
enumerations in Swift are quite different than ObjC, in a very good way :]
Here you need to use:
UIBarButtonItemStyle.Plain
You can also do:
var b = UIBarButtonItem(title: "Continue", style: .Plain, target: self, action: nil)
for maximum simplicity!
Side note: Use let instead of var wherever you can, its better for performance, health, etc!