Swift: Custom Nav Bar Image not firing action on click - swift

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.

Related

How can I reset navigation bar items when coming back from popup?

I have a navigation bar with two buttons as the right bar button items and a text field in the title view. If I tap the text field, a search screen pops up and I can enter texts into the text field. The texts in the text field would set the "resultText" variable in my code below. The button items, including filterItem and mapItem, are well connected with #IBOutlet.
I would like to hide the right bar button items when the text field is not empty. With the code shown below, it works fine initially when I enter texts into the text field. However, when I delete the texts in the text field and then returns from the pop-up, the app crashes because the button items are found nil. I do not understand why it is nil. Am I missing something here?
if !resultText.isEmpty {
navigationItem.rightBarButtonItem = nil
} else {
navigationItem.setRightBarButtonItems([filterItem, mapItem], animated: false)
}
You are adding and removing buttons from the navigation bar, it must be removing reference from view. Try adding it using code -
func addBarButtonItems() {
let filterItemBarButton = UIBarButtonItem(title: "filterItem", style: .plain, target: self, action: #selector(filterItemTapped))
let mapItemBarButton = UIBarButtonItem(title: "mapItem", style: .plain, target: self, action: #selector(mapItemTapped))
navigationItem.rightBarButtonItems = [filterItemTapped, mapItemTapped]
}
func removeBarButtonItems() {
navigationItem.rightBarButtonItems = nil
}
#objc private func filterItemTapped() {
//code
}
#objc private func mapItemTapped() {
//code
}
Call these methods correctly in textField delegate methods.

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 set navigationBar back button image to default symbols?

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

How can I set image location correctly in Swift?

In my app, I am using SWRevealViewController. Currently everything is working OK except for the menu image position.I set up the menu button programatically. Now I need to put it to the correct place.It also needs to work in all screen sizes and needs to be at the same place.
I set up the menu button like this
override func viewDidAppear(animated: Bool) {
let image = UIImage(named: "reveal-icon#2x.png")
tabBarController?.navigationItem.leftBarButtonItem = UIBarButtonItem(title: "", style:
UIBarButtonItemStyle.Plain, target: self.revealViewController(), action: Selector("revealToggle:"))
tabBarController?.navigationItem.leftBarButtonItem?.image = image
}
and here is the result
How do I fix it? I think I need to do it programatically...
And here is the overall layout of my storyboard file.
Thanks for your answers.
This might be happening because you are initializing the UIBarButtonItem with a title. Initialize it with something like this:
let img = UIImage(named: "reveal-icon#2x.png")!.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal)
let leftBarButtonItem = UIBarButtonItem(image: img, style: UIBarButtonItemStyle.Plain, target: self.revealViewController(), action: Selector("revealToggle:"))
tabBarController?.navigationItem.leftBarButtonItem = leftBarButtonItem

How do I make a custom back button and action in SWIFT

I am trying to make a custom back button in swift although it never changes, I am slo trying to make the back button do an action and perform a function. The code where I am at:
var doneButton = UIImage(named: "arrow2.png")
var topLeftButton = UIBarButtonItem(image: doneButton, style: UIBarButtonItemStyle.Plain, target: self, action: Selector("backToList"))
topLeftButton.tintColor = UIColor.whiteColor()
self.navigationItem.backBarButtonItem = topLeftButton //nothing happens
func backToList(){
saveData()// nothing runs
}
I want to be able to change the back button to my own UIImage and make a selector action happens once clicked.
thanks
You can add an UIImage instead of a button with the picture wanted and add a "Tap Gesture Recognizer" by dragging it to the UIImage. Also, don't forget to enable user interaction under the Attributes section of the image (see image below)
After that you control drag your UIImage into the code and create the action with your desired function.