I am using IQKeyboardManager and i added custom action for the done button
txtfield.addDoneOnKeyboard(withTarget: self, action: #selector(doneButtonClicked), titleText: "Next")
IQKeyboardManager.shared().toolbarDoneBarButtonItemText = "Next"
#objc func doneButtonClicked(_ sender: Any) {
// do stuff
}
Here the action gets called and everything works. But the title of the donebutton is DONE (in bluecolor).
The "Next" is show at the middle of the keyboard as titletext. How do i change the text/color of the done button? If i dont add any action to any specific textfield, the second line works perfectly , but anytime i add action, the custom name for done button is ignored.
Any help?
IQKeyboardManager in github
Use the following:
let config = IQBarButtonItemConfiguration(title: "Next", action: #selector(doneButtonClicked))
txtfield.addKeyboardToolbar(withTarget: self, titleText: nil , rightBarButtonConfiguration: config, previousBarButtonConfiguration: nil, nextBarButtonConfiguration: nil)
// any color you like
txtfield.keyboardToolbar.doneBarButton.setTitleTextAttributes([NSAttributedString.Key.foregroundColor: UIColor.red], for: UIControl.State.normal)
Instead of:
txtfield.addDoneOnKeyboard(withTarget: self, action: #selector(doneButtonClicked), titleText: "Next")
what is more? titleText sets toolbar.titleBarButton.title.
I pick some code snip from IQKeyboardManager source code here:
//Title button
toolbar.titleBarButton.title = titleText
while toolbarDoneBarButtonItemText set's doneBarButton.title
some code snip from IQKeyboardManager here:
if let rightConfig = rightBarButtonConfiguration {
var done = toolbar.doneBarButton
if rightConfig.barButtonSystemItem == nil && done.isSystemItem == false {
done.title = rightConfig.title
done.image = rightConfig.image
done.target = target
done.action = rightConfig.action
}
Try this one.
import IQKeyboardManagerSwift
IQKeyboardManager.shared.toolbarDoneBarButtonItemText = "NEXT"
See below picture has the NEXT button in Xcode 10.1.
Related
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.
I am quite new to Swift and I am fascinated to the potential of distinguish these two gesture for a button.
I am writing my first app in xCode and I am near to conclude that. As a last step I want to implement two different actions for a button depending on a long press or a tap.
I have constructed the app as follows. I have several buttons connected to one IBAction and distinguished them using tags.
coming to the tag of the one of the two buttons on which I need the long press action I don't know how to continue.
Do you have some suggestion?
Thank you so much
func longTap() {
if (resultDisplay.text != ""){
storedVariableA = String(result)
eraseAll()
}
}
else if (sender.tag == 20) {
let longPressGesture = UILongPressGestureRecognizer(target: self, action: #selector(self.longTap(_:)))
longPressGesture.minimumPressDuration = 2
sender.addGestureRecognizer(longPressGesture)
}
You can check in your #IBAction for the tag you given in storyboard or programmatically, Please check the below code.
#IBAction func action(_ sender: UIButton) {
if sender.tag == 22 { // check for your desired tag instead of "22"
// add longpress gesture. on sender // sender represents your button.
let longPressGesture = UILongPressGestureRecognizer(target: self, action: #selector(self.handleLongPressGesture(_:)))
longPressGesture.minimumPressDuration = 2 // mention minimum press duration you want user to press.
sender.addGestureRecognizer(longPressGesture)
} else {
}
}
I'm making simple quiz app about countries in Europe, I have a map and three buttons below with names of countries, one of them is correct and of course two incorrect. I want to highlight incorrect (if user click incorrect button) button for red and correct for green, if user click correct button I want to highlight it for green and after maybe 5s come back to the same color that was at first. I know how to change button color but I don't know ho do that for 5s and come back to default color. How can I do that ? Below its code that I use to change button color
UIButtonOutlet.Backgroundcolor.Uicolor.green
But its default green, so I can't set my color.
You can try something like this. First declare one UIButton instance in your class and then scheduledTimer after setting backgroundColor of button for correct's and incorrect and store that button reference with button that you have created first.
var selectedButton = UIButton()
Now use this selectedButton when you are setting button's backgroundcolor.
btnOutlet.backgroundcolor = .green //For correct
btnOutlet.backgroundcolor = .red //For incorrect
self.selectedButton = btnOutlet
//Now scheduled the timer for 5.0 sec
Timer.scheduledTimer(timeInterval: 5.0, target: self, selector: #selector(setButtonBGBack), userInfo: nil, repeats: false)
Add the setButtonBGBack method in your class
func setButtonBGBack() {
self.selectedButton.backgroundcolor = .blue //Set to default here
self.selectedButton = UIButton()
}
Lets say you have your code for changing the color which is in the "changeColor" func:
var correctButton: UIButton
func changeColor() {
if correctButton.backgroundColor == .green {
correctButton.backgroundColor = .lightGray //back to original color
}
}
What this essentially does is changes your correct button color back to its default (whatever that may be) if it is green when the func is called.
Now to use this, we can do a little work inside of the IBAction that is connected to each of your buttons:
#IBAction func buttonClicked(sender: UIButton) {
if sender.titleLabel?.text == "Correct Answer" {
button.backgroundColor = .green
correctButton = button //set the correct button variable so the changeColor func can be used
let timer = Timer.init(timeInterval: 5, target: self, selector: #selector(changeColor), userInfo: nil, repeats: false)
} else if sender.titleLabel?.text == "False Answer 1" || sender.titleLabel?.text == "False Answer 2" {
button.backgroundColor = .red
}
}
So in this code, if you click the button that is the correct answer (you can identify this by tag or other means, but I just decided to use its text) then the color is set to green immediately, and then the correctButton variable is set, but then a timer is initiated that after five seconds will call your changeColor func and then change it back to its original color.
Hope this helps :)
Edit:
Of course, my method assumes that you are using the storyboard to create these buttons. If you are creating them programmatically, then NiravD's method will work better.
In the app I'm making I have a side menu that I used SWRevealViewController template to make. I made my own animated button to be the hamburger menu button so when its pressed the side menu will open. The problem is I can't figure out how to connect my animated button to the SWRevealViewController.
Here's the button code I made.
Animated Button
self.button = HamburgerButton(frame: CGRectMake(0, 0, 30, 30))
self.button.addTarget(self, action: #selector(home.toggle(_:)), forControlEvents:.TouchUpInside)
let refreshButton = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.Refresh,
target: self, action: #selector(home.buttonMethod))
navigationItem.leftBarButtonItem = button
and heres the button that was used for the SWRevealViewController
override func viewDidLoad() {
super.viewDidLoad()
if revealViewController() != nil {
menuButton.target = revealViewController()
menuButton.action = #selector(SWRevealViewController.revealToggle(_:))
view.addGestureRecognizer(self.revealViewController().panGestureRecognizer())
}
}
Ive done a lot of research but just can't find out how to do it. I need the button I made, which is the first code, to be the one to access the SWRevealViewController and to open and close the side menu rather then the button, which is the second code, that came with the SWRevealViewController template. Any help will be Awesome!!
This is how I do it. You can adapt this to your needs.
let singleTap = UITapGestureRecognizer(target: self, action: #selector(tapDetected))
singleTap.numberOfTapsRequired = 1
sideMenuButton.userInteractionEnabled = true
sideMenuButton.addGestureRecognizer(singleTap)
func tapDetected() {
self.revealViewController().revealToggle(self)
}
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.