I have an application that has a navigation bar
I'm adding a cancel button with a listener in my view controller:
navigationItem.leftBarButtonItem = UIBarButtonItem(title: "Cancel", style: .plain, target: self, action: #selector(addTapped))
but when I run my app
I don't see my cancel button on the navigation bar anymore
what am I doing wrong?
your code should work but can you make sure you did the following.
1. Put the code under the view.didLoad{} in your view controller
class YourViewController: UIViewController {
override func viewDidload() {
super.viewDidLoad()
navigationItem.leftBarButtonItem = UIBarButtonItem(title: "Cancel", style: .plain, target: self, action: #selector(addTapped))
}
}
2, Make sure that you have no image covered on your screen because it can hide nav bar
Related
I'm trying to add a UIBarButtonItem programmatically.
let navigation = UINavigationController()
let rightBarButton = UIBarButtonItem(title: "LogIn", style: .plain, target: self, action: #selector(logInPressed))
navigation.navigationItem.rightBarButtonItem = rightBarButton
And also made a selector function for testing.
#objc func logInPressed() {
print("go to login")
}
Unfortunately that does not work - bar button is not visible on navigation bar in simulator.
Checked with a breakpoint, rightBarButtonItem exists.
Probably issue can be caused by creating bar button from app coordinator, not from child VC.
Could please anyone help to troubleshoot this issue?
Simulator screenshot
you are adding UIBarButtonItem to a new instance of NavigationController. so it will not appear there.
so in your view controller which you want to handle right navigation bar , under one of these methods:
override func viewDidLoad()
or
override func viewWillAppear
add:
let rightBarButton = UIBarButtonItem(title: "LogIn", style: .plain, target: self, action: #selector(logInPressed))
self.navigationController?.navigationItem.setRightBarButton(rightBarButton, animated: true)
Issue was caused by creating bar button not from child VC, but coordinator.
Bar buttons are stored in navigationItem property of UIViewController.
there is one screen that i don't want to show back button symbols.
i create a empty image and change the navigation bar back button image.(code like following)
navigationController?.navigationBar.backIndicatorImage = UIImage(named: "mainicon_gray-13")
navigationController?.navigationBar.backIndicatorTransitionMaskImage = UIImage(named: "mainicon_gray-13")
navigationItem.backBarButtonItem = UIBarButtonItem(title: "demo", style: .plain, target: nil, action: nil)
screen like following Picture
But all of backBarButtonItem changed, i need to set backBarButtonItem to default symbols "<" when i back to the view.
Is there any way to set navigation bar back button image to default image?
i want the navigationBar like following picture
following is the way that i found without change back button settings to do same event.
use leftBarButtonItem and set popViewController to go back before screen.
override func viewDidLoad() {
navigationItem.leftBarButtonItem = UIBarButtonItem(title: "test", style: .plain, target: self,action: #selector(backViewBtnFnc))
}
#objc func backViewBtnFnc(){
self.navigationController?.popViewController(animated: true)
}
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 :)
I am trying to set a custom BarButtonItem to my NavigationBar of my DetailsVC in my UISplitViewController with these two lines of code:
let barbuttonitem = UIBarButtonItem(title: "Hi", style: .plain, target: nil, action: nil)
navigationItem.rightBarButtonItems = [barbuttonitem]
but somehow nothing happens. When i create a new project and add these 2 lines of codes for my navBar it works, but in my real App it doesn't.
Thanks for your help
I have a UIToolbar, but I have no idea how to positioning it..
I would add UIBarButtonItems to it, and positioning them. The toolbar showing up, but i can't change it size, and i can't position the buttons in it.
override func viewDidAppear(animated: Bool) {
self.navigationController?.setToolbarHidden(false, animated: true)
self.navigationController?.toolbar.frame = CGRectMake(50, 50, 50, 50)
self.navigationController?.toolbar.sizeToFit()
let plusImg = UIImage(named: "navbar_icon-02.png")?.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal)
let plusButton = UIBarButtonItem(image: plusImg, style: UIBarButtonItemStyle.Plain, target: self, action: "postLeft:")
let filterButton = UIBarButtonItem(title: "Feed filter", style: UIBarButtonItemStyle.Plain, target: nil, action: nil)
let leftButton = UIBarButtonItem(title: "Button 3 tst", style: UIBarButtonItemStyle.Plain, target: nil, action: nil)
var toolbarButtons = [filterButton]
toolbarButtons.append(plusButton)
toolbarButtons.append(leftButton)
self.toolbarItems = toolbarButtons
}
There are two possible problems you might be having (I can't quite tell, from the way you've asked the question).
One problem might be that you are not in a navigation interface - you simply have a "loose" toolbar. Thus, the toolbar referred to through self.navigationController? is not your toolbar, and setting self.toolbarItems has no effect - those things are only for when you are in a navigation interface and the toolbar in question is the navigation controller's toolbar.
The other problem might be that you are in a navigation interface, in which case the toolbar position is not up to you - it's up to the navigation controller, which will always place it at the bottom of the screen.
So, either you need to be in a navigation interface so that the toolbar is the navigation controller's toolbar (and then your code will work, except for the positioning part), or else you can use a "loose" tolbar, in which case you need your code to refer to your toolbar, in which case you are free to position it, and you can give it items by setting its items.