使用 StackEdit 编写。
SelectMultipleButtons
一个支持多选/单选的组按钮,通过 Swift 编建,自动布局。React Native 的GitHub仓库。
演示(由 GIPHY CAPTURE 捕获)
安装
编辑 podfile
pod 'selectmultiplebuttons', '~> 0.0.3'
查看帧层次结构
说明
您必须采用 SelectMultipleButtonsDelegate
协议。该协议类似于 UITableViewDelegate
和 UITableViewDataSource
,提供 SelectMultipleButtons 构建按钮和管理按钮选择所需的信息...
注释:返回多少个按钮
func numberOf(selectMultipleButtons: SelectMultipleButtons) -> Int
注释:返回的按钮在哪个索引位置
func buttonOf(selectMultipleButtons: SelectMultipleButtons, atIndex index: Int) -> UIButton
注释:设置最大选中数
默认您可以选择所有按钮
如果将值设为1,selectmultiplebuttons将呈现单选选择。
@objc optional func maximumNumberSelectedOf(selectMultipleButtons: SelectMultipleButtons) -> Int
配置,包含以下项目:
边缘间距(顶部、首行、底部、尾行)
按钮之间的空间(按钮之间的垂直空间和水平空间)
func styleConfigOf(selectMultipleButtons: SelectMultipleButtons) -> StyleConfig
按钮的单次点击事件
@objc optional func didSingleTapOf(selectMultipleButtons: SelectMultipleButtons, atIndex index: Int) -> Void
索引选择和变化事件
@objc optional func indexesSelectedOf(selectMultipleButtons: SelectMultipleButtons, didChange indexes: [Int]) -> Void
代码快照
导入模块
import selectmultiplebuttons
初始化一个selectMultipleButtons
//宽度是SelectMultipleButtons视图的最大宽度
//不关心高度,执行SelectMultipleButtons.load()后,高度会自动计算。
//必须在将其添加到父视图之前执行SelectMultipleButtons.load()。
let selectMultipleButtons1 = SelectMultipleButtons.init(frame: CGRect.init(x: 0, y: tip1.frame.origin.y + tip1.frame.height, width: view.frame.width, height: 0))
selectMultipleButtons1.delegate = self
selectMultipleButtons1.load()
view.addSubview(selectMultipleButtons1)
遵守所需协议
func numberOf(selectMultipleButtons: SelectMultipleButtons) -> Int {
return buttonTitleArray1.count
}
func buttonOf(selectMultipleButtons: SelectMultipleButtons, atIndex index: Int) -> UIButton {
let button = UIButton.init()
button.setTitle(buttonTitleArray1[index], for: .normal)
//set none selected state
button.setTitleColor(.lightGray, for: .normal)
button.setBackgroundColor(.groupTableViewBackground, for: .normal)
//set selected state
button.setTitleColor(.white, for: .selected)
button.setBackgroundColor(UIColor.init(red: 0x3B/0xFF, green: 0x67/0xFF, blue: 0xBC/0xFF, alpha: 1), for: .selected)
//default button is selected
button.isSelected = true
return button
}
func styleConfigOf(selectMultipleButtons: SelectMultipleButtons) -> StyleConfig {
let edgeSpace = EdgeSpace.init(top: 10, leading: 10, bottom: 10, trailling: 10)
let betweenSpace = BetweenSpace.init(horizontalSpace: 5, verticalSpace: 5)
return StyleConfig.init(edgeSpace: edgeSpace, betweenSpace: betweenSpace, systemFontSize: 18)
}
许可证
selectmultiplebuttons采用MIT许可。