I Have created two BarButtonItems and added to the rightbarbutton, first bar button action only calling, second bar button action not calling, can any one help me
let saveBarButton = UIBarButtonItem(title: "Save", style: .Done, target: self, action: #selector(saveClicked(_:)))
let addVitalBarButton = UIBarButtonItem(barButtonSystemItem: .Add, target: self, action: #selector(addVital(_:)))
self.navigationItem.setRightBarButtonItems([addVitalBarButton,saveBarButton], animated: true)
only addVitalBarButton action calling, if I changed the items order
self.navigationItem.setRightBarButtonItems([saveBarButton,addVitalBarButton], animated: true)
the above case only saveBarButtonAction calling
func saveClicked(sender: UIBarButtonItem) {
print("SaveClicked selected ")
}
func addVital(sender: UIBarButtonItem){
print("Add vital selected")
}
For left bar button I added, it is working fine, In right bar button only it is not calling
can any one help me figure out why the second bar button action is not calling?
Related
I'm trying to remove the text "Back" from the navigation back button, leaving just the back chevron, but everything I'm trying is not working. For example if I add something like the following, obtained from previous answers to the same question, to viewDidLoad:
navigationItem.backBarButtonItem = UIBarButtonItem(title: "go away", style: .plain, target: nil, action: nil)
or
navigationController?.navigationBar.backBarButtonItem = UIBarButtonItem(title: "go away", style: .plain, target: nil, action: nil)
Then when the view appears it's still showing "< Back" in the navigation bar.
Here's what the views look like within captured within viewDidAppear.
Image:1
Try this code snippet hope it will help you
happy coding =)
override func viewDidLoad() {
DispatchQueue.main.async {
if let navBar = self.navigationController?.navigationBar {
navBar.backItem?.title = ""
}
}
}
You should create a left button and set the action to return to the rootViewController.
In viewDidLoad:
let leftButton = UIBarButtonItem(title: "<", style: .plain, target: self, action: #selector(back(_ :)))
self.navigationItem.leftBarButtonItem = leftButton
You are changing the wrong thing. You use this code here to change the title for the back button.
navigationItem.leftBarButtonItem = UIBarButtonItem(title: "Back", style: .done, target: self, action: #selector(handleBack))
By doing this you need to add a selector for the button as well. Cause if you click the back button nothing will happen. This is how you would do that.
#objc private func handleBack() {
navigationController?.popViewController(animated: true)
}
Hope this helps.
Alternatively, from Interface Builder, you can set previous UIViewController's Back Button on Navigation Item to " " (not empty string, space):
I have two buttons inside of a toolbar that is positioned at the bottom of the screen. I would like to center those buttons within the toolbar. Everything I have found is focused on centering buttons within a nav bar and I am unsure of how to approach this issue. What am I missing?
This is the UI I am attempting to emulate:
Adding a flexible space to the left and right of the buttons centered the buttons inside of the toolbar. Thanks to Midhun MP for the tip.
Thks #Andrew, it works
but for someone who wants it from code here is the example in Swift 5.0
override func viewDidLoad() {
super.viewDidLoad()
navigationController?.setToolbarHidden(false, animated: true)
let spaceItemLeft = UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: self, action: nil)
let nextItem = UIBarButtonItem(title: "NEXT", style: .plain, target: self, action: #selector(nextTapped))
let spaceItemRight = UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: self, action: nil)
toolbarItems = [spaceItemLeft, nextItem, spaceItemRight]
}
#objc func nextTapped() {
print("NEXT Tapped")
}
I have added a navigation bar to the top of a view controller. I am trying to control whether a button is visible based a condition, but I am having trouble adding the button. So far I have,
var addButton: UIBarButtonItem = UIBarButtonItem(title: "test", style: .done, target: self, action: #selector(addTapped))
override func viewDidLoad() {
super.viewDidLoad()
let boool = true
if boool {
self.navigationItem.rightBarButtonItem = self.addButton
}
else {
self.navigationItem.rightBarButtonItem = nil
}
}
func addTapped(sender: AnyObject) {
print("hjxdbsdhjbv")
}
I believe it is not working properly because I have added a navigation bar into the VC, instead of using a navigation controller and working with the bar there. I was wondering if there was a way to work with this navigation bar.
It’s simple. Put this line of code to the viewDidLoad:
self.navigationItem.rightBarButtonItem = UIBarButtonItem(title: "test", style: .done, target: self, action: #selector(addTapped))
Updated for Swift 4 or later:
A custom function:
#objc func action(sender: UIBarButtonItem) {
// Function body goes here
}
(Custom) Right bar button item:
self.navigationItem.rightBarButtonItem = UIBarButtonItem.init(title: "some_text", style: .done, target: self, action: #selector(self.action(sender:)))
(Custom) Left bar button item:
self.navigationItem.leftBarButtonItem = UIBarButtonItem.init(title: "some_text", style: .done, target: self, action: #selector(self.action(sender:)))
Also you can add a system bar button items something like this: UIBarButtonItem.SystemItem Defines system-supplied images for bar button items: .add, .done, .cancel, .edit, .save, .compose, .reply, .organize and more.
(System) Right bar button item:
self.navigationItem.rightBarButtonItem = UIBarButtonItem.init(barButtonSystemItem: UIBarButtonItem.SystemItem.add, target: self, action: #selector(self.action(sender:)))
(System) Left bar button item:
self.navigationItem.leftBarButtonItem = UIBarButtonItem.init(barButtonSystemItem: UIBarButtonItem.SystemItem.add, target: self, action: #selector(self.action(sender:)))
let rightBarButtonItem = UIBarButtonItem.init(image: UIImage(named: "EditImage"), style: .done, target: self, action: #selector(ViewController.call_Method))
self.navigationItem.rightBarButtonItem = rightBarButtonItem
You say you added a UINavigationBar to your view controller via storyboard, but looking at the code you provided there is no outlet connection to your navigation bar in IB.
In order to access self.navigationItem your view controller must be embedded in a UINavigationController or be part of a hierarchy which is. Unless you have a need for a custom navigation bar on an individual view controller, I suggest removing that from Interface Builder, then making sure either the view controller in question is embedded in a UINavigationController or it is being pushed onto the navigation stack from another controller which is embedded in a navigation controller and then you should see your UIBarButtonItem.
Firstly, you need to connect you navigation bar to an IBOutlet so that you can refer to it in your code. After that, this code should work:
let navigationItem = UINavigationItem(title: "Title")
navigationItem.rightBarButtonItem = self.addButton
navigationItem.hidesBackButton = true
self.navigationBar.pushItem(navigationItem, animated: false)
Swift 4.2;
Add viewController
override func viewDidLoad() {
super.viewDidLoad()
self.addNavigationBarButton(imageName: "ic_back", direction:.left)
}
Add Class your API or Utility Class
public func addNavigationBarButton(imageName:String,direction:direction){
var image = UIImage(named: imageName)
image = image?.withRenderingMode(.alwaysOriginal)
switch direction {
case .left:
self.navigationItem.leftBarButtonItem = UIBarButtonItem(image: image, style:.plain, target: nil, action: #selector(goBack))
case .right:
self.navigationItem.rightBarButtonItem = UIBarButtonItem(image: image, style:.plain, target: nil, action: #selector(goBack))
}
}
#objc public func goBack() {
self.navigationController?.popViewController(animated: true)
}
public enum direction {
case right
case left
}
tested in Xcode 10.2, swift 5.0;
First, I have have embedded my ViewController in UINavigationController in IB.
Then in ViewDidLoad include these lines
self.title = "orange"
self.navigationItem.rightBarButtonItem = UIBarButtonItem(barButtonSystemItem: .add, target: self, action: #selector(changeLayout)).
Note - accessing title, or adding button through navigation controller did not work. For example : setting title - Self.navigationcontroller.navigationItem.title did not work ,
let backButton = UIBarButtonItem(
title: "",
style: UIBarButtonItemStyle.Plain,
target: nil,
action: nil
);
self.navigationController!.navigationBar.tintColor = UIColor.whiteColor()
self.navigationController!.navigationBar.topItem!.backBarButtonItem = backButton;
i have use above code and i want to add google Interstitial ads in back button of navigation item.
my requirement is when i press back from second viewcontroller ads.
should would be come and when i close ads. first view controller
should open.
thanks
While creating back button, you should define selector/action methods for that backbutton.
let backButton = UIBarButtonItem(
title: "",
style: UIBarButtonItemStyle.Plain,
target: self,
action: #selector(backButtonPressed)
)
in same controller you can have method which will execute after clicking back button
func backButtonPressed(){
Print("Back Button Pressed")
//Write code for showing google ads here.
}
i solved my problem by adding this below code in secondViewController. and its work perfectly.
override func viewWillDisappear(animated: Bool) {
if (interstitial.isReady && showAd) {
showAd = false
print("iterstitalMain is ready")
interstitial.presentFromRootViewController(parentViewController!)
self.createAndLoadInterstitial()
}
else{
}
}
How can I change back button place to right bar button of embedded navigation and change the the icon from < to >?
My suggestion is:
Hide back button:
navigationItem.hidesBackButton = YES
Add a right button with an image arrow:
let backButton = UIBarButtonItem(title: ">", style: .Plain, target: self, action: #selector(backTapped))
navigationItem.rightBarButtonItem = backButton
the action of backTapped will pop the view controller from the navigation controller stack.
Let me know in the comments if this approach can resolve your problem.
I use :
let newView = sabtEnsheabViewController(nibName: "sabtEnsheabViewController", bundle: nil)
self.navigationController?.pushViewController(newView, animated: false)
but it show back button to another view
use this :
let newView = EnsheabSabteNamViewController(nibName: "EnsheabSabteNamViewController", bundle: nil)
self.presentViewController(newView, animated: false, completion: nil)
it doesn't show navigationBar anymore
I use this way for change back button position.
//put this code in viewDidLoad()
navigationItem.hidesBackButton = true
navigationItem.rightBarButtonItem = UIBarButtonItem(title: " > ", style: .done,target: self, action: #selector(addTapped))
//function to handle back navigation
#objc func addTapped(){
navigationController?.popToRootViewController(animated: true)
}