swift中實現單選按鈕
想要實現單選按鈕Radio button:

swift radio button
swift radio button lib
shamasshahid/SSRadioButtonsController: A Radio Button Controller class for iOS written in Swift
swift – How to create NSRadioButton Group in Xcode 7 OSX – Stack Overflow
iphone – Best radio-button implementation for IOS – Stack Overflow
iPhone UIButton tutorial: Radio Buttons – Mobisoft
Implementing Radio Button-Like Behavior in iOS Applications – DZone Mobile
TNSwiftyCheckboxGroup, create checkbox groups in Swift
gui design – What is the iOS alternative to a radio button? – User Experience Stack Exchange
先去試試:
DavydLiu/DLRadioButton: A highly customizable Radio Button for iOS
最后用:
//
// LeftLabelRightRadioButtonTableViewCell.swift
// SalesApp
//
// Created by licrifan on 16/6/10.
// Copyright © 2016年 licrifan. All rights reserved.
//
import UIKit
import Cartography
import DLRadioButton
let LeftLabelRightRadioButtonTableViewCellId:String = "LeftLabelRightRadioButtonTableViewCellId"
let SingleRadioButtonWidth:CGFloat = 60
let RadioButtonUnselectedImage = UIImage(named: "radio_button_unselected")!
let RadioButtonSelectedImage = UIImage(named: "radio_button_selected")!
class LeftLabelRightRadioButtonTableViewCell: LeftLabelRightTextfieldTableViewCell {
init(editable:Bool = false,
reuseIdentifier: String? = nil,
leftLabelText:String = "",
isMandatory:Bool = false,
optionList:[String] = [String](),
curSelectedIdx:Int = 0
) {
if editable {
super.init(editable: false, reuseIdentifier: reuseIdentifier ?? LeftLabelRightRadioButtonTableViewCellId, leftLabelText: leftLabelText, isMandatory: isMandatory)
self.rightTextfield.hidden = true
let firstRadioButton = DLRadioButton()
radioButtonCommonSetup(firstRadioButton)
firstRadioButton.setTitle(optionList[0], forState: UIControlState.Normal)
self.contentView.addSubview(firstRadioButton)
//for debug
firstRadioButton.backgroundColor = UIColor.greenColor()
constrain(firstRadioButton) {firstRadioButton in
firstRadioButton.left == firstRadioButton.superview!.left + RightTextfiledPaddingLeftToParent
firstRadioButton.centerY == firstRadioButton.superview!.centerY
firstRadioButton.width == SingleRadioButtonWidth
}
var otherButtonList = [DLRadioButton]()
for curIdx in 1..<optionList.count {
let curSelection = optionList[curIdx]
gLog.verbose("[\(curIdx)] curSelection=\(curSelection)")
let curRadioButton = DLRadioButton()
radioButtonCommonSetup(curRadioButton)
curRadioButton.setTitle(curSelection, forState: UIControlState.Normal)
self.contentView.addSubview(curRadioButton)
let currentButtonPaddingLeft:CGFloat = RightTextfiledPaddingLeftToParent + (CGFloat(curIdx)*SingleRadioButtonWidth)
gLog.verbose("currentButtonPaddingLeft=\(currentButtonPaddingLeft)")
constrain(curRadioButton) {curRadioButton in
curRadioButton.left == curRadioButton.superview!.left + currentButtonPaddingLeft
curRadioButton.centerY == curRadioButton.superview!.centerY
curRadioButton.width == SingleRadioButtonWidth
}
otherButtonList.append(curRadioButton)
}
firstRadioButton.otherButtons = otherButtonList
} else {
//….
}
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
func logSelectedButton(radioButton: DLRadioButton) {
if (radioButton.multipleSelectionEnabled) {
for button in radioButton.selectedButtons() {
gLog.verbose(String(format: "%@ is selected.\n", button.titleLabel!.text!));
}
} else {
gLog.verbose(String(format: "%@ is selected.\n", radioButton.selectedButton()!.titleLabel!.text!));
}
}
func radioButtonCommonSetup(radioButton:DLRadioButton) {
gLog.verbose("radioButton=\(radioButton)")
//for debug
radioButton.backgroundColor = UIColor.yellowColor()
// radioButton.titleLabel?.textAlignment = .Left
radioButton.titleLabel!.font = RightTextFieldTextFont
radioButton.setTitleColor(RightTextFieldTextColor, forState: UIControlState.Normal)
radioButton.icon = RadioButtonUnselectedImage
radioButton.iconSelected = RadioButtonSelectedImage
radioButton.marginWidth = 0
radioButton.iconStrokeWidth = 2
// radioButton.iconSize = RadioButtonUnselectedImage.size.width
radioButton.indicatorSize = 1
radioButton.contentHorizontalAlignment = UIControlContentHorizontalAlignment.Left
radioButton.addTarget(self, action: #selector(self.logSelectedButton(_:)), forControlEvents: UIControlEvents.TouchUpInside)
}
}
//1.3 customer gender cell
let customerGenderCell = LeftLabelRightRadioButtonTableViewCell(editable: self.editable, leftLabelText: "性別", optionList: ["男", "女", "變態"], curSelectedIdx: self.curCustomerItem.genderIndex)
基本達到了效果:


選中按鈕輸出的log:
2016-06-14 21:29:07.726 [Verbose] [main] [LeftLabelRightRadioButtonTableViewCell.swift:109] logSelectedButton > 男 is selected.
2016-06-14 21:29:08.600 [Verbose] [main] [LeftLabelRightRadioButtonTableViewCell.swift:109] logSelectedButton > 女 is selected.
2016-06-14 21:29:09.310 [Verbose] [main] [LeftLabelRightRadioButtonTableViewCell.swift:109] logSelectedButton > 變態 is selected.
2016-06-14 21:29:10.252 [Verbose] [main] [LeftLabelRightRadioButtonTableViewCell.swift:109] logSelectedButton > 男 is selected.
->但是,此處的,左邊的button按鈕和右邊的title的間距
嘗試了N種設置:
radioButton.titleLabel?.textAlignment = .Left
radioButton.marginWidth = 0
radioButton.iconStrokeWidth = 2
// radioButton.iconSize = RadioButtonUnselectedImage.size.width
radioButton.indicatorSize = 1
radioButton.contentHorizontalAlignment = UIControlContentHorizontalAlignment.Left
都沒用。
-》感覺是,這個庫沒寫好,有bug啊。。。
搞得沒法實現我要的,文字緊挨着左邊的按鈕的效果
轉載請注明:在路上 » [基本解決]swift中實現單選按鈕
