I have a file called UIElements.swift
which contains some extension I want to use throughout my app.
They've worked great so far. Until I created a new viewController, and I can't get any of them to work in that or any other viewControllers I make. They only work in my first viewController?
Here's the code for the extension file:
import UIKit
extension UIImage {
//create image from UIColor, to use for buttons
class func imageWithColor(color:UIColor?) -> UIImage! {
let rect = CGRectMake(0.0, 0.0, 1.0, 1.0);
UIGraphicsBeginImageContextWithOptions(rect.size, false, 0)
let context = UIGraphicsGetCurrentContext();
if let color = color {
color.setFill()
}
else {
UIColor.whiteColor().setFill()
}
CGContextFillRect(context, rect);
let image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
}
Obviously I have more extensions in the file, but this is the function I was trying to call first when I noticed it didn't work.
Here is the code that doesn't work, in a file called Login_VC.swift
import UIKit
class LoginVC: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
var signInButton = UIButton()
signInButton.setTranslatesAutoresizingMaskIntoConstraints(false)
signInButton.setTitle("SIGN IN", forState: .Normal)
signInButton.titleLabel!.font = UIFont(name: "BebasNeue-Bold", size: 50)
signInButton.setBackgroundImage(UIImage.imageWithColor(UIColor.colorWithHex("#12D99E")), forState: .Normal)
self.view.addSubview(signInButton)
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
The above code returns this following error:
'UIImage.Type' does not have a member named 'imageWithColor'
and here is a similar button in the first view controller I made called LandingPageVC.swift
(this code works fine)
import UIKit
class LandingPageVC: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
var signUpButton: UIButton = UIButton()
signUpButton.setTranslatesAutoresizingMaskIntoConstraints(false)
signUpButton.setTitle("Sign Up Today", forState: .Normal)
signUpButton.titleLabel!.font = UIFont(name: "BebasNeue-Bold", size: 50)
signUpButton.setBackgroundImage(UIImage.imageWithColor(UIColor.colorWithHex("#12D99E")), forState: .Normal)
signUpButton.setBackgroundImage(UIImage.imageWithColor(UIColor.colorWithHex("#0DB07F")), forState: .Highlighted)
signUpButton.layer.shadowColor = formulaShadowColor.CGColor
signUpButton.layer.shadowOffset = CGSizeMake(3, 3)
signUpButton.layer.shadowRadius = 0
signUpButton.layer.shadowOpacity = 1.0
signUpButton.addTarget(self, action: "signUp:", forControlEvents: .TouchUpInside)
signUpButton.addTarget(self, action: "signUpHighlighted:", forControlEvents: .TouchDown)
signUpButton.addTarget(self, action: "signUpReset:", forControlEvents: .TouchDragExit)
self.view.addSubview(signUpButton)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
I can't see anything I've done differently in either file.
yet when I start writing UIImage.i
-> it suggests imageWithColor
in the first view controller, but it doesn't show up in any other file.
I didn't do anything to import this class in either file. As it's just an extension of UIImage
, it should be imported with UIKit
.
I also tried adding public
to the class, which didn't do anything.
can anyone explain why this function works in one file, but not the other?