Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
222 views
in Technique[技术] by (71.8m points)

关于swift3.0的类目问题

在swift中,我在使用类目调用的时候出现了一个错误,求解答:

Use of instance member 'creatImageWithColor' on type 'UIImage'; did you mean to use a value of type 'UIImage' instead?

代码是 我直接创建了file.swift
在里面加了类目

extension UIImage {
    // 颜色直接换换成图片
    open func creatImageWithColor(_ color: UIColor, _ size: CGSize) -> UIImage {
        let rect = CGRect(x: 0, y: 0, width: size.width, height: size.height)
        // create a 1 by 1 pixel context
        UIGraphicsBeginImageContextWithOptions(rect.size, false, 0)
        color.setFill()
        UIRectFill(rect)
        let image: UIImage? = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
        return image!
    }
}

然后在VC中调用

        let editBtn = UIButton.init(type: .custom)
        editBtn.frame = CGRect(x:10, y:8, width:SCREEN_WIDTH - 20, height:44)
        editBtn.layer.cornerRadius = 8
        editBtn.layer.masksToBounds = true
        editBtn.setTitle("编辑资料", for: .normal)
        editBtn.setTitleColor(UIColor.white, for: .normal)
        editBtn.setBackgroundImage(UIImage.creatImageWithColor(UIColor.black, CGSize(width:10, height:10)), for: .normal)
          // 这里出现了 开始发的那个警告 求怎么解决
        editBtn.addTarget(self, action: #selector(editNow), for: .touchUpInside)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

你的类函数/方法前要加关键词 static,才能直接用类名调用。


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...