Scope
iOS: 10.0
Eureka: 3.0
How to configure properties of the presented view on eg: MultipleSelectorRow<T>?
Code
From the Example project, here's one MultipleSelectorRow
<<< MultipleSelectorRow<Emoji>() {
$0.title = "MultipleSelectorRow"
$0.options = [💁🏻, 🍐, 👦🏼, 🐗, 🐼, 🐻]
$0.value = [👦🏼, 🍐, 🐗]
}.onPresent { from, to in
to.navigationItem.rightBarButtonItem = UIBarButtonItem(barButtonSystemItem: .done, target: from, action: #selector(RowsExampleViewController.multipleSelectorDone(_:)))
}.cellSetup { cell, row in
cell.tintColor = .orange
}
This code produces a selector view where the tint is default, rather than whatever the tint can be setup for the Row's cell itself (in this case, .orange):
The solution is to configure the presented VC in the onPresent callback. There are more things that can be configured, just need to dive in to the classes.
Configuration
Simply define selectableRowCellSetup in a similar way to [cellSetup][1]
...
.onPresent { from, to in
to.navigationItem.rightBarButtonItem = UIBarButtonItem(barButtonSystemItem: .done, target: from, action: #selector(RowsExampleViewController.multipleSelectorDone(_:)))
to.selectableRowCellSetup = { cell, row in
cell.tintColor = .red
}
}
...
Result
Related
I want to hide/show a UIBarButtonItem when a segmentedControl changes, this is my code:
#objc fileprivate func handleSegmentedChange() {
switch segmentedControl.selectedSegmentIndex {
case index0:
// Set the proper rightBarButtonItems, in the first load this bar button items will be nil, this is why we have to check first
self.navigationItem.rightBarButtonItems?.append(UIBarButtonItem(image: #imageLiteral(resourceName: "Filter2"), style: .plain, target: self, action: #selector(openBottomSheet)))
default:
self.navigationItem.rightBarButtonItems?.remove(at: 0)
}
}
However is not updating the views (hiding or showing anything).
Note I've also tried setting the rightBarButtonItems to nil before adding or removing items, however is not working.
How can I accomplish the desired effect?
If rightBarButtonItems is nil before you try to append or remove items to/from it, then nothing will happen as you cannot append or remove items to/from a non-existent array.
Instead of appending/removing to/from rightBarButtonItems, try just setting it directly to the items you want it to be, like this:
#objc fileprivate func handleSegmentedChange() {
switch segmentedControl.selectedSegmentIndex {
case 0:
let barButtonItem = UIBarButtonItem(image: #imageLiteral(resourceName: "Filter2"),
style: .plain,
target: self,
action: #selector(openBottomSheet))
navigationItem.rightBarButtonItems = [barButtonItem]
// Note: If you're just dealing with one bar button item,
// you could also just use `navigationItem.rightBarButtonItem` like:
// navigationItem.rightBarButtonItem = barButtonItem
default:
navigationItem.rightBarButtonItems = nil // or `= []`
// Note: If you're just dealing with one bar button item,
// you could also just use `navigationItem.rightBarButtonItem` like:
// navigationItem.rightBarButtonItem = nil
}
}
I am developing a TODOList(iOS application) but there is a problem i.e. how can I add dropdown list is uicollectionview cell.
Means when view did load, collection view loaded, there should be a dropdown in every single cell
There's no any inbuilt dropdown functionality in iOS but you can do it by using UITableView or by using 3rd party library.
I suggest you to try this. DropDown
define this globally.
let dropDown = DropDown()
If you want to customise dropDown you can use this.
func customizeDropDown() {
DropDown.appearance().cellHeight = 40
DropDown.appearance().backgroundColor = UIColor.white
DropDown.appearance().selectionBackgroundColor = Colors.purpleColor
DropDown.appearance().cornerRadius = 5
DropDown.appearance().textColor = Colors.NavTitleColor
DropDown.appearance().shadowColor = (UIColor.init(hexString: "1AD691")?.withAlphaComponent(0.0))!
DropDown.appearance().shadowOpacity = 0.9
DropDown.appearance().shadowRadius = 0
DropDown.appearance().animationduration = 0.25
}
In cellForItemAt you need to add action on your dropdown button like this.
cell.btnDropdown.tag = indexPath.item
cell.btnDropdown.addTarget(self, action: #selector(btnDropDownTapped), for: .touchUpInside)
Once you tapped on any button from UICollectionViewCell below method will call where you need to pass anchorView.
#IBAction func btnDropDownTapped(_ sender: UIButton) {
self.dropDown.anchorView = sender // The view to which the drop down will appear on
self.dropDown.bottomOffset = CGPoint(x: 0, y: sender.bounds.height) //Top of drop down will be below the anchorView
self.dropDown.dataSource = ["First", "Last", "Second", "Third"] // Static array you need to change as per your requirement
self.dropDown.selectionAction = { [unowned self] (index, item) in
print(item) // **NOTE: I AM JUST PRINTING DROPDOWN SELECTED VALUE HERE, YOU NEED TO GET `UICollectionViewCell` HERE YOU NEED TO SET VALUE INSIDE CELL LABEL OR YOU CAN SET SELECTED DROPDOWN VALUE IN YOUR MODEL AND RELOAD COLLECTIONVIEW**
self.collectionView.reloadData()
}
self.dropDown.show()
}
If you have UITextField in your UICollectionViewCell then you can try this code inside textFieldShouldBeginEditing delegate.
NOTE: I AM JUST PRINTING DROPDOWN SELECTED VALUE HERE, YOU NEED TO GET UICollectionViewCell HERE YOU NEED TO SET VALUE INSIDE CELL LABEL OR YOU CAN SET SELECTED DROPDOWN VALUE IN YOUR MODEL AND RELOAD COLLECTIONVIEW
iOS doesn't have any native control for drop downs.You can use pickers instead.
See like below :
Here is the code to add pickerView.
let picker: UIPickerView
picker = UIPickerView(frame: CGRectMake(0, 200, view.frame.width, 300))
picker.backgroundColor = .whiteColor()
picker.showsSelectionIndicator = true
picker.delegate = self
picker.dataSource = self
let toolBar = UIToolbar()
toolBar.barStyle = UIBarStyle.Default
toolBar.translucent = true
toolBar.tintColor = UIColor(red: 76/255, green: 217/255, blue: 100/255, alpha: 1)
toolBar.sizeToFit()
let doneButton = UIBarButtonItem(title: "Done", style: UIBarButtonItemStyle.Plain, target: self, action: "donePicker")
let spaceButton = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.FlexibleSpace, target: nil, action: nil)
let cancelButton = UIBarButtonItem(title: "Cancel", style: UIBarButtonItemStyle.Plain, target: self, action: "donePicker")
toolBar.setItems([cancelButton, spaceButton, doneButton], animated: false)
toolBar.userInteractionEnabled = true
textField1.inputView = picker
textField1.inputAccessoryView = toolBar
There are lots of libraries added to github for cool picker controls:
Actionsheet PickerView
Selectionmenu
Please follow steps to perform your operation
Add UITableView in UICollectionViewCell.
Make the hidden property of UITableView to true
collectionViewCell.dropDownTableView.isHidden=true
When the user select option to display dropdown, reload UITableView data and set hidden property of UITableView to false.
How to add UITableView to UICollectionViewCell
I didn't find in the documentation how to add ability to select multiple rows (see the attached screenshot). I know how to do it with ordinary UITableView, but when I use Eureka and call tableView.setEditing(true, animated: true) nothing happens. Is there any way to implement this with Eureka?
Use a MultipleSelectorRow, for example:
<<< MultipleSelectorRow<Emoji>() {
$0.title = "MultipleSelectorRow"
$0.options = [💁🏻, 🍐, 👦🏼, 🐗, 🐼, 🐻]
$0.value = []
}
.onPresent { from, to in
to.navigationItem.rightBarButtonItem = UIBarButtonItem(barButtonSystemItem: .done, target: from, action: #selector(YourViewController.multipleSelectorDone(_:)))
}
Use it to present a to view controller with multiple choices.
I've been trying to implement this toolbar, where only the 'Next' button is enabled when the top textField is the firstResponder and only the 'Previous' button is enabled when the bottom textField is the firstResponder.
It kind of works, but i need to execute my own code by accessing previous, next and done buttons action methods in other classes(like delegates)
Thanks in advance for your suggestions..
extension UIViewController {
func addInputAccessoryForTextFields(textFields: [UITextField], dismissable: Bool = true, previousNextable: Bool = false) {
for (index, textField) in textFields.enumerated() {
let toolbar: UIToolbar = UIToolbar()
toolbar.sizeToFit()
var items = [UIBarButtonItem]()
if previousNextable {
let previousButton = UIBarButtonItem(image: UIImage(named: "Backward Arrow"), style: .plain, target: nil, action: nil)
previousButton.width = 30
if textField == textFields.first {
previousButton.isEnabled = false
} else {
previousButton.target = textFields[index - 1]
previousButton.action = #selector(UITextField.becomeFirstResponder)
}
let nextButton = UIBarButtonItem(image: UIImage(named: "Forward Arrow"), style: .plain, target: nil, action: nil)
nextButton.width = 30
if textField == textFields.last {
nextButton.isEnabled = false
} else {
nextButton.target = textFields[index + 1]
nextButton.action = #selector(UITextField.becomeFirstResponder)
}
items.append(contentsOf: [previousButton, nextButton])
}
let spacer = UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: nil, action: nil)
let doneButton = UIBarButtonItem(barButtonSystemItem: .done, target: view, action: #selector(UIView.endEditing))
items.append(contentsOf: [spacer, doneButton])
toolbar.setItems(items, animated: false)
textField.inputAccessoryView = toolbar
}
}
}
I am calling this from other class as :
let field1 = UITextField()
let field2 = UITextField()
addInputAccessoryForTextFields([field1, field2], dismissable: true, previousNextable: true)
Although I'm not 100% convinced I understand your question, here goes:
From other classes, you want to call the actions of your buttons, but your actions are set to UITextField.becomeFirstResponder and UIView.endEditing.
Rather than call these methods directly, create your own methods the actions should call, and put these calls into those methods.
In addInputAccessoryForTextFields(...) change the previousButton's target and action to:
previousButton.target = self
previousButton.action = #selector(handlePreviousButton)
Now add the new method:
#objc func handlePreviousButton()
{
// you'll need to associate the previous button to a specific text field
// and hang onto that association in your class, such as in a property named textFieldRelatedToPreviousButton.
self.textFieldRelatedToPreviousButton.becomeFirstResponder()
}
Now you can call handlePreviousButton() directly from elsewhere in your class, if you wish, or even from other classes.
Update
I just noticed you're extending UIViewController. So you can't add storage by adding a property. You can add storage via objc_setAssociatedObject and then get it via objc_getAssociatedObject, however, to get around this. See this SO or this SO for details on that. So you can, for example, "attach" the textField to your previousButton so that you can access it via the handlePreviousButton() method you add to your extension. And you can pass in the previousButton as a parameter (the sender) to handlePreviousButton() too.
Update 2
Another approach to consider is to use the button's tag property to store the tag value of the related textField. (i.e. each button and its related textField would have the same tag value). So in handlePreviousButton(sender:UIBarButtonItem) you loop through all the UITextField children of your self.view and locate the one whose tag matches sender.tag . Then you can do what you need to that UITextField.
I created two UIBarButtonItem buttons inside viewDidLoad to be grouped on the right side of the navigation bar so that when certain condition is satisfied, one will be enabled and the other disabled and vice versa.
the buttons work fine except recognizing the condition and being enabled and disabled based on the condition.
override func viewDidLoad() {
super.viewDidLoad()
var btnDone: UIBarButtonItem = UIBarButtonItem(
barButtonSystemItem: .Done,
target: self,
action: "btnDoneFunction")
btnDone.style = UIBarButtonItemStyle.Done
let btnAdd: UIBarButtonItem = UIBarButtonItem(
barButtonSystemItem: .Add,
target: self,
action: "btnAddFunction")
var buttons = [btnAdd, btnDone]
self.navigationItem.rightBarButtonItems = buttons
if checked.count >= 1 {
btnDone.enabled = true
btnAdd.enabled = false
} else {
btnDone.enabled = false
btnAdd.enabled = true
}
}
checked is an array that holds row number of checked rows in the table inside didSelectRowAtIndexPath.
I tried moving the condition part:
if checked.count >= 1 {
btnDone.enabled = true
btnAdd.enabled = false
} else {
btnDone.enabled = false
btnAdd.enabled = true
}
inside of didSelectRowAtIndexPath, but i can not access the buttons there.
Is there any way I can implement this? I just want to check if there are any rows checked in the table and disable the 'Add' button and enable the 'Done' button. If no row is checked, the reverse.
This is inside a UITableViewController class.
You are creating the buttons in viewDidLoad so the scope of the variable is just for that function, you need to declare it inside the class.
class myClass{
lazy var btnDone:UIBarButtonItem = {
var done: UIBarButtonItem = UIBarButtonItem(
barButtonSystemItem: .Done,
target: self,
action: "btnDoneFunction")
return done
}()
func anotherFunc(){
if checked.count >= 1 {
btnDone?.enabled = true
} else {
btnDone?.enabled = false
}
}
}
I create a generic example above that declares a btnDone as a class variable with lazy initialization, that means it will be create just as need