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.
Related
In iOS 13 back button title color not dimmed whenever we open popover or present other controller. Other part of application grayed out but not back button.
also tintColorDidChange function is not available for UIBarButtonItem so is there any way to do grayed out all back button titles.
Note: Back button image is being grayed.
Have you try by changing tint color of bar button?
I got same problem and solved by using
self.navigationController?.navigationBar.titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.clear]
self.navigationController?.navigationBar.tintColor = .red // put your desire color
self.navigationItem.backBarButtonItem = UIBarButtonItem(title: "", style: .plain, target: nil, action: nil)
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.
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.
I'm trying to get my side menu (swreveal) to show when clicking a UIImageView with a tap gesture or button. I've got the image view and tap all set up and the tap gesture linked to an action. Getting this functionality is easy when using a bar button item but I can't seem to get it to function when using a plain old button or image view.
Here's how I do it for a bar button item:
barButtonItem!.target = self.revealViewController()
barButtonItem!.action = Selector("revealToggle:")
This obviously doesn't work for buttons or ImageViews because they don't have a member of target or action. Would really appreciate some assistance on this one!
To add target to UIButton you can use this:
...
let button = UIButton()
button.addTarget(self, action: "someAction:", forControlEvents: .TouchUpInside)
...
func someAction(sender: UIButton!) {
...
}
For adding action to press on UIImageView you can use UITapGestureRecognizer like this:
...
let tap = UITapGestureRecognizer(target: self, action: Selector("someAction"))
tap.numberOfTapsRequired = 1
image.userInteractionEnabled = true
image.addGestureRecognizer(tap)
...
func someAction() {
...
}
I want an action to be done once when someone swipes within a button.
My current code is as follows:
let recogniser = UISwipeGestureRecognizer(target: self, action: "didTapButton2:")
recogniser.direction = .Up
button.addGestureRecognizer(recogniser)
func didTapButton2(sender: UIGestureRecognizer!) {
//Here I want to be able to recognise which button this was sent from (they are all tagged)
let button = sender. as UIButton //Gives an error
I need to use gesture recognisers as opposed to UIControlEvents as I need the event to only fire once. Using this makes the event fire loads of times - just need it to go once:
button.addTarget(self, action: "didTapButton2:", forControlEvents: .TouchDragInside)
Does anyone have a solution? Thanks
A UIGestureRecognizer has a property called view which is the view that it is attached to. View is declared as an optional, so you need to unwrap it:
if let button = sender.view as? UIButton {
// use button
if button.tag == 10 {
// handle button tagged with 10
}
}