• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

swift之mac中实现单选按钮

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

想要实现单选按钮Radio button:

swift radio button

swift radio button lib

shamasshahid/s-s-radioButtonsController: 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.\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中实现单选按钮


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
[Swift]LeetCode270.最近的二分搜索树的值$ClosestBinarySearchTreeValue发布时间:2022-07-13
下一篇:
Swift-@IBDesignable和@IBInspectable发布时间:2022-07-13
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap