This question already has answers here:
Swift UIColor initializer - compiler error only when targeting iPhone5s
                                
                                    (2个答案)
                                
                        
                                4年前关闭。
            
                    
我知道这不是一种合适的方法,但是出于学习的目的,我有一个选择器控件,该控件具有四个组件,每个组件有255行,以基于RGB-alpha更改“视图”背景色。
我知道UIColor(red: <#CGFloat#>, green: <#CGFloat#>, blue: <#CGFloat#>, alpha: <#CGFloat#>)接受四个浮点数作为每种颜色的参数,但出现错误:
找不到类型为'UIColor'的初始化程序,该初始化程序接受类型为'(red:Float,green:Float,blue:Float,alpha:Float)'的参数列表
怎么了?

func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {

    var newbackgroundColor = UIColor()

    var redCode: Float = 0.0
    var greenCode: Float = 0.0
    var blueCode: Float = 0.0
    var alphaCode: Float = 0.0

    if component == 0 {
        switch row {
        case 0..<256:
            redCode = Float(row/255)
            newbackgroundColor = UIColor(red: redCode, green: greenCode, blue: blueCode, alpha: alphaCode)
        default:
            break
        }

    } else if component == 1 {
        switch row {
        case 0..<256:
            greenCode = Float(row/255)
            newbackgroundColor = UIColor(red: redCode, green: greenCode, blue: blueCode, alpha: alphaCode)
        default:
            break
        }

    } else if component == 2{
        switch row {
        case 0..<256:
            blueCode = Float(row/255)
            newbackgroundColor = UIColor(red: redCode, green: greenCode, blue: blueCode, alpha: alphaCode)
        default:
            break
        }

    } else {
        switch row {
        case 0..<256:
            alphaCode = Float(row/255)
            newbackgroundColor = UIColor(red: redCode, green: greenCode, blue: blueCode, alpha: alphaCode)
        default:
            break
        }

    }

    self.view.backgroundColor = newbackgroundColor
}

最佳答案

使用CGFloat而不Float

当您收到这样的错误时,请检查API文档

关于swift - UIColor初始化程序(找不到可接受的…的'UIColor'类型的初始化程序)错误,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/30837531/

10-13 05:03