This repository has been archived by the owner on Apr 19, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 209
How to set default selection in segmented control #147
Comments
IMHO it is a bug, but I have a workaround for you ;-) Just use the appearance dictionary to set the index of the segmented control. (Code from the example of the project) row = FormRowDescriptor(tag: Static.segmented, type: .segmentedControl, title: "Priority")
row.configuration.selection.options = ([0, 1, 2, 3] as [Int]) as [AnyObject]
row.configuration.selection.optionTitleClosure = { value in
guard let option = value as? Int else { return "" }
switch option {
case 0:
return "None"
case 1:
return "!"
case 2:
return "!!"
case 3:
return "!!!"
default:
return ""
}
}
//this alone has just no effect
row.value=2 as AnyObject?
row.configuration.cell.appearance = ["titleLabel.font" : UIFont.boldSystemFont(ofSize: 30.0), "segmentedControl.tintColor" : UIColor.red]
//you can either set the appearance dictionary as a whole
row.configuration.cell.appearance = ["titleLabel.font" : UIFont.boldSystemFont(ofSize: 30.0), "segmentedControl.tintColor" : UIColor.red, "segmentedControl.selectedSegmentIndex": 2 as AnyObject]
//or set the value in the dicionary afterwards
row.configuration.cell.appearance["segmentedControl.selectedSegmentIndex"]=2 as AnyObject
//the best way IMHO is to set the index via the value (in that way the row.value is the model for the segmented control, like it should be)
row.configuration.cell.appearance["segmentedControl.selectedSegmentIndex"]=row.value
section4.rows.append(row) Running that code the segmented control is pre filled with your set value |
I posted your questions (and my answer) on stackoverflow. |
Thorsten, thank you kindly for your response. |
Likewise, would like to present a slider control. The following has no affect, i.e. the default position is to the far left, or 0:
Would appreciate also how to set the default slider value. |
Disregard. This can be accomplished similarly, e.g.
|
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
The included sample form contains a segmented control. I attempt to set the default selected value using "row.value = 1 as AnyObject", though this does not seem to work. How does one set the default selection for the segmented control.
Thanks again for all of your efforts.
The text was updated successfully, but these errors were encountered: