swift中实现单选按钮

Posted sundaymac

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了swift中实现单选按钮相关的知识,希望对你有一定的参考价值。

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

-》frederik-jacques/TNSwiftyCheckboxGroup: A Swift component based on UICollectionView to create checkbox groups with various styles.

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. ", button.titleLabel!.text!));

            }

        } else {

            gLog.verbose(String(format: "%@ is selected. ", 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中实现单选按钮

以上是关于swift中实现单选按钮的主要内容,如果未能解决你的问题,请参考以下文章

iOS 中单选按钮的实现

iphone中自定义单选按钮的默认选择

vue2.0中实现单选,全选,购物车加减计算等效果

IOS 的最佳单选按钮实现

java swing中实现列表中加入单选按钮,单选按钮发生变化时能触发事件

Swift 单选按钮 - 复选框 - Swift 3