Switch - Tai-Kimura/SwiftJsonUI GitHub Wiki
Switch
class: SJUISwitch inherits: UISwitch
Platform Support
- ✅ UIKit: Full support (All attributes available)
- ✅ SwiftUI: Full support (Maps to Toggle with equivalent functionality)
- ✅ Jetpack Compose: Full support (Maps to Switch composable)
Attributes for Switch
| attribute name | UIKit | SwiftUI | Compose | XML | type in json | details | remarks |
|---|---|---|---|---|---|---|---|
| value | ✅ | ✅ | ✅ | ✅ | boolean/string | Initial on/off state. Can be boolean or @{variableName} | |
| tint | ✅ | ✅ | ✅ | ❌ | string | switch.onTintColor. Color when in "on" position | |
| onValueChange | ✅ | ✅ | ✅ | ❌ | string | UIControlEvents.valueChanged event selector | |
| thumbTintColor | ✅ | ✅ | ✅ | ❌ | string | Color of the switch thumb/handle | |
| offTintColor | ✅ | ❌ | ✅ | ❌ | string | Color when in "off" position | UIKit only |
| enabled | ✅ | ✅ | ✅ | ✅ | boolean/string | view.isEnabled. Can be boolean or @{variableName} |
Properties for Switch
Static Properties
open class var viewClass: SJUISwitch.Type
This property will be used to decide which class to inflate with createFromJSON method. You should define the view's class on this property when you create classes inheriting from SJUISwitch.
Inherited Properties
SJUISwitch inherits all properties from UISwitch and SJUIView, including:
isOn: Bool- The current state of the switch (on/off)onTintColor: UIColor?- Color when switch is in "on" positionthumbTintColor: UIColor?- Color of the switch thumb/handletintColor: UIColor!- Color when switch is in "off" position- Touch and gesture handling from SJUIView
- Layout constraint management
- Event handling capabilities
Functions for Switch
Static Methods
public class func createFromJSON(attr: JSON, target: Any, views: inout [String: UIView]) -> SJUISwitch
Factory method called when the switch is created from JSON file. Automatically configures the switch with the provided attributes including:
onValueChangeevent handler for value change eventstintcolor configuration for the "on" state- Override this method when you create classes inheriting from SJUISwitch class.
Event Handling
SJUISwitch supports value change events through:
- The
onValueChangeattribute for UIControl.Event.valueChanged - Automatic target-action setup with the specified selector
- Integration with UISwitch's built-in value change notifications
Inherited Methods
SJUISwitch inherits all methods from UISwitch and SJUIView, including:
setOn(_:animated:)- Programmatically set switch state with animation- Value change event handling
- Touch and gesture handling from SJUIView
- Layout constraint management
- UIControl event system integration
Usage Examples
Basic Switch Configuration
{
"type": "Switch",
"id": "notificationSwitch",
"width": "wrapContent",
"height": "wrapContent",
"tint": "#007AFF",
"onValueChange": "toggleNotifications"
}
With Data Binding
{
"type": "Switch",
"id": "settingsSwitch",
"width": "wrapContent",
"height": "wrapContent",
"data": [
{
"name": "isEnabled",
"class": "Bool",
"defaultValue": false
}
],
"binding": "@{isEnabled}",
"tint": "#34C759",
"onValueChange": "updateSettings"
}