Setting the text of a BarButtonItem on the NavigationBar? - swift

Id like to remove an icon and add text to BarButtomItem, but im not sure how to do it. here is my currrent code:
let logoutButton = UIBarButtonItem(barButtonSystemItem: .Action, target: self, action: #selector(SettingsViewController.logout))
self.navigationItem.setRightBarButtonItem(logoutButton, animated: false)
I thought i could just do UIBarButtonItem(text: "sign out", ... but I guess that doesn't work.

you can connect your UIBarButtonItem from the UI.
#IBOutlet weak var logoutButton: UIBarButtonItem!
It should look like this when you connect it.
Setting of that button can look like this
And finally put this line to ViewDidLoad func:
//Logout button
logoutButton.title = "Sign out" //This is the button title
:) It should work.

This is an old post but in case somebody else's need this, you can actually create a bar button item with just text:
let logoutButton:UIBarButtonItem = UIBarButtonItem(title:"Sign out",
style:UIBarButtonItemStyle.plain,
target:self,
action:#selector(SettingsViewController.logout))
self.navigationItem.setRightBarButtonItem(logoutButton, animated:false)

Related

UIKit: Adding text to NavigationBar

I'm diving into UIKit and feel some misunderstanding after using SwiftUI. By following course from Paul Hudson there is some challenges in the end of the project. One of this it's try showing the player’s score in the navigation bar. I try to solve this case but closest solution that I found from Apple's documentation it's:
let scoreButton = UIBarButtonItem(title: NSLocalizedString("Score: \(score)", comment: ""),
style: .done,
target: self,
action: nil)
navigationItem.rightBarButtonItem = scoreButton
But I think adding Button instead of simple Text is unnecessary, I don't need button behavior. Trying to add UITextView() to NavigationBar from storyboard did't work out...
Question: how to add text(better programmatically) to Navigation Bar? something like .toolbarItem with ToolbarItemPlacement: .navigationBarTrailing in SwiftUI. Is it possible or it's a UIKit restriction using only rightBarButtonItem?
You can use titleView property to show a text in the NavigationBar
override func viewDidLoad() {
super.viewDidLoad()
let view = UIView()
view.addSubview(titleLabel)
view.addSubview(scoreLabel)
navigationItem.titleView = view
}
I found solution how to add text to NavBar and yes it's restriction of UIKit thats you have to use UIBarButtonItem
let scoreView = UILabel()
scoreView.text = "Score: \(score)"
let scoreButton = UIBarButtonItem(customView: scoreView)
navigationItem.rightBarButtonItem = scoreButton

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

Using UINavigationItem on UITabBarController

I have a project with a UITabBarController and a couple of views. Sort of like this:
In this project, the tab bar controller is called by tapping the Tab button on the UIViewController. I would like to get rid of the back button with "Title", and replace it with an "X" icon. When tapped, the "X" icon would dismiss the UITabBarController and return to the caller. I do this all of the time on UINavigationController using a UINavigationItem, but that does not appear to work in this situation. I drag the UINavigationItem to the view and it allows it, but it does not show up on the view and any UIBarButtonItem that I drag and drop on it do not appear.
Is there a way to actually do this? I'd even be ok with leaving the existing back button as it is and just getting rid of "Title"
I figured it out right after posting the question. Just a bit more research is all it took.
To fix this, add the following to the UITabBarController code.
override func viewDidLoad() {
super.viewDidLoad()
let buttonImage = UIImage(named: "back")
let leftButton = UIBarButtonItem(image: UIImage(named: "back"), style: .plain, target: self, action: #selector(dismissTabBar))
leftButton.tintColor = UIColor.black
self.navigationItem.leftBarButtonItem = leftButton
}
#IBAction func dismissTabBar() {
self.navigationController?.popToRootViewController(animated: true)
}
This gives me a black back button with my image. When I tap it, it brings me back to the calling 'UIViewController.

How to create a plus symbol bar button item

How do you create UIBarButtonItem as a plus symbol? I tried giving it the title "+", but I would like it to look more like the plus symbol you would normally create if you used storyboard. Which doesn't have a background or an underline.
It's one of the provided bar button system items.
let addBarButton = UIBarButtonItem(barButtonSystemItem: .add, target: YOUR_TARGET, action: YOUR_SELECTOR)
I think this is what you are looking for:
Swift 4.2 Example
Here is how you do it:
let addBarButton = UIBarButtonItem(barButtonSystemItem: .add, target: self, action: #selector(addUser))
override func viewDidLoad() {
super.viewDidLoad()
self.navigationItem.rightBarButtonItem = addBarButton
}
#objc private func addUser() {
// Some code, e.g.
let storyboard = UIStoryboard(name: "CreateNewUser", bundle: nil)
let vc = storyboard.instantiateViewController(withIdentifier: "createUser") as! CreateUserVC
self.navigationController?.pushViewController(vc, animated: true)
}
IMPORTANT NOTE: There is a little problem though which many users have pointed out and took me many hours to figure out as well (for iOS 11, 12) that you must put the self.navigationItem.rightBarButtonItem = addBarButton inside the viewDidLoad function otherwise the button's selector won't work. This has something to do, as per other users on StackOverflow, with tap gestures. In my code, one of my view controllers have two buttons assigned outside of viewDidLoad function but there is also use a function to hide the keyboard, which probably causes the buttons to work fine. However, in view controllers of two other projects where I don't show or hide keyboard, the #selector for programatically set navigation bar buttons don't work unless they are defined within viewDidLoad function as in the example above. I hope this tip will save you tons of time in case you face this problem.
Swift 3.0
let addBarButton = UIBarButtonItem(barButtonSystemItem: .add, target: self, action: #selector(ViewController.openSearch))

Swift: Custom Nav Bar Image not firing action on click

I have a navigation bar on my viewcontroller.
I have created a custom image (of a cog) and have got that to show right:
//Add bar item
var image = UIImage(named: "settingsIcon")
image = image?.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal)
NavigationBar.rightBarButtonItem = UIBarButtonItem(image: image, style: UIBarButtonItemStyle.Plain, target: nil, action: "addTapped")
I added the action to the above code:
action: "addTapped"
Then I created a function:
func addTapped() {
print("Tapped")
}
When I run the app, the image shows fine. When I click the image nothing happens, not even an error.
Am I doing something blatantly wrong here?
Try setting the target to self instead of nil.