I have five pickers in total. I want to create a done button for each one of them. I want to use a different selector for each one to to do different actions. To set up the done buttons I was trying to use the same method for all of them, but I do not know how to pass a function argument into the selector in Swift.
func createDoneButton(txtField: UITextField, donePressed: /* What do I need here? */) {
let toolbar = UIToolbar() // create toolbar
toolbar.sizeToFit() // toolbar fits the size of the screen
let doneBtn = UIBarButtonItem(barButtonSystemItem: .done, target: nil, action: #selector(donePressed)) // action when the done button was pressed
toolbar.setItems([doneBtn], animated: true)
txtField.inputAccessoryView = toolbar
}
You can find the answer in your question :)
Simply use Selector parameter type, and no need #selector()
func createDoneButton(txtField: UITextField, donePressed: Selector){
let toolbar = UIToolbar() // create toolbar
toolbar.sizeToFit() // toolbar fits the size of the screen
let doneBtn = UIBarButtonItem(barButtonSystemItem: .done, target: nil, action: donePressed) // action when the done button was pressed
toolbar.setItems([doneBtn], animated: true)
txtField.inputAccessoryView = toolbar
}
Related
I am using UIPickerView and it's being triggered by pushing a button like so:
#IBAction func communityButtonPressed(_ sender: Any) {
//Set the picker view delegate to self
pickerView.delegate = self
//Set the picker view data source to self
pickerView.dataSource = self
//Define the tool bar
let toolBar = UIToolbar()
//Set tool bar style
toolBar.barStyle = UIBarStyle.default
//Set tool bar translucent to true
toolBar.isTranslucent = true
//Resizes and moves the tool bar view so it just encloses its subviews.
toolBar.sizeToFit()
//Define done button for tool bar
let doneButton = UIBarButtonItem(title: "Done", style: UIBarButtonItem.Style.plain, target: self, action: #selector(Dashboard.donePicker))
//Define space inbetween the buttons for tool bar
let spaceButton = UIBarButtonItem(barButtonSystemItem: UIBarButtonItem.SystemItem.flexibleSpace, target: nil, action: nil)
//Define cancel button for tool bar
let cancelButton = UIBarButtonItem(title: "Cancel", style: UIBarButtonItem.Style.plain, target: self, action: #selector(Dashboard.cancelPicker))
//Assign tool bar buttons to tool bar
toolBar.setItems([cancelButton, spaceButton, doneButton], animated: false)
//Set the tool bar to be interactive
toolBar.isUserInteractionEnabled = true
//Add the tool bar to the picker view
pickTextField.inputAccessoryView = toolBar
//Add picker view to view
view.addSubview(pickTextField)
//Assign the picker view to picker text field
pickTextField.inputView = pickerView
//Set picker text field as first responder
pickTextField.becomeFirstResponder()
}
Now I am trying to set a selected row in the viewDidAppear but it's not setting the selected row.
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
//Call method to get the user bundle settings
registerSettingsBundle()
//Apply the users bundle settings
updateDisplayFromDefaults()
//Clear the picker options
pickOption = [String]()
//For each community in the app delegate community array
for item in (appDelegate.communityDescriptionArray?["kCommunityDescriptions"] as! [AnyObject])
{
//Add it to the picker options
pickOption?.append(item as! String)
}
self.selectedIndex = (appDelegate.communityDescriptionArray?["kCommunityDescriptions"]!.index(of: UserDefaults.standard.string(forKey: "community")!))!
self.pickerView.selectRow(3, inComponent: 0, animated: true)
}
I did noticed that my viewDidAppear is loaded first then numberOfRowsInComponent, then titleForRow, is that why the selected row is not taking?
I am trying to make a custom back button using this code:
let back = UIImage(named: "header_backarrow")
let backView = UIImageView(image: back)
let backItem = UIBarButtonItem(customView: backView)
navigationItem.leftBarButtonItem = backItem
I want the navigation item to perform this code:
func dismissManual() {
dismiss(animated: true, completion: nil)
}
I have tried many things like following this Stack Overflow post: Execute action when back bar button of UINavigationController is pressed
I have also tried making it a navigationItem.backBarButtonItem; however, nothing seems to work. Some things show the correct custom image, but do not work as a button; on the other hand, some work as a button, but do not show the correct image.
Does anybody know how I can show the correct image and make the item work as a button? Thanks.
Do it as follows:
override func viewDidLoad() {
super.viewDidLoad()
let back = UIImage(named: "header_backarrow")
let backView = UIImageView(image: back)
backView.isUserInteractionEnabled = true
let tap = UITapGestureRecognizer(target: self, action: #selector(dismissManual))
backView.addGestureRecognizer(tap)
let backItem = UIBarButtonItem(customView: backView)
navigationItem.leftBarButtonItem = backItem
}
#objc func dismissManual() {
print("print----")
// dismiss(animated: true, completion: nil)
}
Add gesture to backView it will work!
It's similiar to this question IOS - Swift - adding target and action to BarButtonItem's customView
Swift 4.1
The issue is that UIImage does not have tap recognition. You will have to add a tap gesture recognizer to your backView.
lazy var singleTap: UITapGestureRecognizer = {
let singleTap = UITapGestureRecognizer(target: self, action: #selector(tapDetected))
singleTap.numberOfTapsRequired = 1
return singleTap
}()
// Actions
#objc func tapDetected() {
dismiss(animated: true, completion: nil)
}
If you show your code, with some screenshots I can give more help if this doesn't solve the issue.
You are creating your UIBarButtonItem incorrectly. You do not need the image view.
Do it as follows:
let back = UIImage(named: "header_backarrow")
let backItem = UIBarButtonItem(image: back, style: .plain, target: self, action: #selector(dismissManual))
navigationItem.leftBarButtonItem = backItem
#objc func dismissManual() {
dismiss(animated: true, completion: nil)
}
Note that the function must be marked with #objc.
Depending on your image and how you want it displayed, you may need to create the image as follows:
let back = UIImage(named: "header_backarrow").withRenderingMode(.alwaysOriginal)
Another option is to create a UIButton with the image and setup to call your dismissManual function. Create the UIBarButtonItem with the button as the custom view.
But it's easier to create a standard UIBarButtonItem when all you have is a simple image or a simple string.
let barButtonItem = UIBarButtonItem(image: UIImage(named: "backImgs"),
style: .plain,
target: self,
action: #selector(menuButtonTapped))
// Adding button to navigation bar (rightBarButtonItem or leftBarButtonItem)
self.navigationItem.rightBarButtonItem = barButtonItem
// Private action
#objc fileprivate func menuButtonTapped() { // body method here }
Check out this, it may help Thanks.
Twitter Registration Page
Hi everyone I have some cushions about UItoolBar on swift 3/4
I'm trying to make an singIn design like twitter,
1) i create toolbar with code;
let toolBar = UIToolbar()
toolBar.sizeToFit()
let flexibleSpace = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.flexibleSpace, target: nil, action: nil)
let doneButton = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.done, target: self, action: #selector(self.doneClicked))
toolBar.setItems([flexibleSpace, doneButton], animated: false)
kullaniciAdi.inputAccessoryView = toolBar
eposta.inputAccessoryView = toolBar
sifre.inputAccessoryView = toolBar
but I can't add custom buttons on that. Like changing the "Done" text in my language.
2) So I created manually using Storyboard a toolBar with inside a button and Flexible Space. But ı need to "attach" that toolbar to the keyboard, so when i start to edit the textfield the toolbar will automatically show up. NOTE: it will be fantastic if the toolbar will show too when not-editing textField. Like that:
My app trying too attach toolbar
THANK YOU ALL
In Code
let sonrakiButton = UIBarButtonItem(title: "Sonraki", style: UIBarButtonItemStyle.plain, target: self, action: #selector(self.sendDoneBtnAction))
For more info, read this link
I am currently working on making the register section of an iOS app in Swift 3.0.2.
Here's a gif
of the problem I am having. Watch the toolbar above the keyboard as the segue occurs. It flashes to a darker shade before it fully loads. This occurs whether or not the keyboard is active throughout the process (ViewController has toolbar as inputAccessoryView).
Here is the toolbar implementation (it's the same for each View).
fileprivate let toolbar = { () -> UIToolbar in
let toolbar = UIToolbar()
toolbar.barStyle = .default
toolbar.isTranslucent = true
toolbar.tintColor = UIColor(netHex: Constant.Color.darkerCrimsonColor)
toolbar.isUserInteractionEnabled = true
toolbar.sizeToFit()
return toolbar
}()
override var inputAccessoryView: UIView? {
return toolbar
}
fileprivate func setupToolbar() {
let otherButton = UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: nil, action: nil)
let doneButton = UIBarButtonItem(title: "Next", style: UIBarButtonItemStyle.done, target: self, action: #selector(nextViewController))
toolBar.setItems([otherButton, otherButton, doneButton], animated: true)
nameTextField.inputAccessoryView = toolbar
}
The entire app is made programmatically. I remade the essentials of the project again through Storyboard and ran into the same problem.
My question is: is this intended behavior? If not, what can I do about this? Is there a way to suppress this behavior completely?
Thanks in advance for your time and consideration!
From past few days i am trying to generate a popover for programmatically added UIBarButtonItem but i couldn't succeed. Added to this, i even want this popover to be presented with few images sequentially which are clickable. The following is the code of how i generated a UIBarButtonItem programmatically
func imagerendering(){
let barbuttonimage = UIImage(named: "app")?.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal)
let button1 = UIBarButtonItem(image: barbuttonimage, style: .Plain, target: self, action: nil)
self.navigationItem.leftBarButtonItem = button1
let attachButtonImage = UIImage(named: "icon-Attach")?.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal)
let attachButton = UIBarButtonItem(image: attachButtonImage, style: UIBarButtonItemStyle.Plain, target: self, action: nil)
self.navigationItem.setRightBarButtonItems([menuButton,attachButton], animated: true)
let fixedSpace:UIBarButtonItem = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.FixedSpace, target: nil, action: nil)
fixedSpace.width = 5.0
self.navigationItem.rightBarButtonItems = [menuButton, fixedSpace, attachButton]
self.navigationController?.navigationBar.titleTextAttributes = [NSFontAttributeName:UIFont(name: "Avenir-Black", size: 16)!]
}
I have presented my desired output in an image as a link below. please go through it and let me know whether what i am desiring is possible or not.
desired output image
To add an extra bar item on navigation bar using storyboard, you can refer these answers
After that, set a popover from storyboard(you know it well as you said) and then put required buttons with actions in your popop view. You can set image for a button instead of title text.
Look into the WYPopoverController library. It gives you the functionality you're looking for.