RadioButton
是 SwiftUI 缺失的单选按钮视图。它使用了与 SwiftUI 的 Picker
radioGroup
样式相同的方法,该样式仅在 macOS 上可用。
✅ macOS 10.15+
✅ Xcode 11.0
✅ Swift 5+
✅ iOS 14+
✅ tvOS 14+
✅ watchOS 7+
RadioButton
可通过 Swift Package Manager 获取。
使用 Xcode 11,前往 File -> Swift Packages -> Add Package Dependency
并输入 https://github.com/lukluca/RadioButton
或手动添加依赖项
dependencies: [
.package(url: "https://github.com/lukluca/RadioButton", from: "1.1.0")
]
使用 RadioButton
非常简单,特别是与 Swift 的 enum
类型结合使用
enum City: String, CaseIterable, Identifiable {
case milano
case roma
case palermo
var id: RawValue {
rawValue
}
}
struct ContentView: View {
@State private var city = City.milano
var body: some View {
RadioButton(title: "Cities:",
itemTitle: \.rawValue,
isSelected: $city)
}
}
您的模型必须实现 CaseIterable
、Hashable
和 Identifiable
协议,这是必需的。
有关更多配置,请参阅 RadioButtonExample 文件夹,其中包含一个非常简单的应用程序,展示了所有可能的场景。
最新版本是 1.2.0
。
从这个版本开始,您可以使用垂直或水平对齐方式。
RadioButton
的 1.0.0
版本包含非常有限的功能集,基本上没有任何特别的功能 😄。
可以通过实现以下任务来扩展它
☘️ 添加单元/UI 测试;
☘️ 创建视图修饰符以设置通用配置(字体、颜色等); ☘️ ... 更多其他改进。
👨🏻🔧 欢迎通过创建 pull request 为 RadioButton
做出贡献,请遵循以下指南
RadioButton
👨💻 @lukluca
📄 RadioButton
在 MIT 许可证下可用。有关更多信息,请参阅 LICENSE 文件。