swift 单选按钮ios

Posted

tags:

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

//
//  IMRadioButton.swift
//  Intermedium
//
//  Created by Jota Melo on 02/03/17.
//  Copyright © 2017 iOasys. All rights reserved.
//

import UIKit

@IBDesignable
class IMRadioButton: IMCheckboxButton {
    
    var extraLabel: UILabel!
    
    @IBInspectable var extraText: String? {
        didSet {
            self.createExtraLabel()
            self.createExtraLabelConstraints()
        }
    }
    
    @IBInspectable var checkboxInsideColor: UIColor? {
        didSet {
            self.checkboxInside.backgroundColor = checkboxInsideColor
        }
    }
    
    override var checkboxCornerRadius: CGFloat {
        get {
            return self.checkboxSize / 2
        }
    }
    
    override func createCheckboxInside() {
        self.checkboxInside = IMView()
        self.checkboxInside.cornerRadius = (self.checkboxSize - (self.insideBorder * 2)) / 2
        self.checkboxInside.backgroundColor = UIColor.clear
        self.checkboxInside.translatesAutoresizingMaskIntoConstraints = false
        self.checkbox.addSubview(self.checkboxInside)
    }
    
    func createExtraLabel() {
        self.extraLabel = UILabel()
        self.extraLabel.text = self.extraText
        self.extraLabel.font = UIFont(name: "GothamPro", size: 18)
        self.extraLabel.textColor = Colors.textDefault
        self.extraLabel.translatesAutoresizingMaskIntoConstraints = false
        self.addSubview(self.extraLabel)
    }
    
    func createExtraLabelConstraints() {
        let extraLabelTopConstraint = NSLayoutConstraint(item: self.extraLabel, attribute: .top, relatedBy: .equal, toItem: self.checkbox, attribute: .bottom, multiplier: 1, constant: 11)
        let extraLabelCenterXConstraint = NSLayoutConstraint(item: self.extraLabel, attribute: .centerX, relatedBy: .equal, toItem: self.checkbox, attribute: .centerX, multiplier: 1, constant: 0)
        let extraLabelBottomConstraint = NSLayoutConstraint(item: self.extraLabel, attribute: .bottom, relatedBy: .equal, toItem: self, attribute: .bottom, multiplier: 1, constant: 0)
        self.addConstraints([extraLabelTopConstraint, extraLabelCenterXConstraint, extraLabelBottomConstraint])
    }
    
    override func handleTap() {
        self.isSelected = !self.isSelected
        if self.extraLabel != nil {
            self.extraLabel.textColor = self.isSelected ? Colors.orange : Colors.textDefault
            self.extraLabel.font = self.isSelected ? UIFont(name: "GothamPro-Medium", size: 18) : UIFont(name: "GothamPro", size: 18)
        }
    }
    
}

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

我需要帮助使用 Swift 在视图控制器中创建两个单选按钮

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

swift中实现单选按钮

Swift tableview 单元格单选按钮实现

swift 单选按钮

Swift为单选按钮创建自定义逻辑