I want to programatically create an image view in a function. This will hold the background of the image view and some logos - then in all my other image views I can just all the function and I wan't have to do the same thing on each one.
I created a blank swift file and put the following in it:
import Foundation
import UIKit
func background() {
var imageView : UIImageView
imageView = UIImageView(frame:CGRectMake(10, 50, 100, 300));
imageView.image = UIImage(named:"image.jpg")
self.view.addSubview(imageView)
}
The only error I got was on the self.view.addSubview(imageView)
line. The error message was Use of unresolved identifier 'self'
. I tried just removing the word self so the line said this instead view.addSubview(imageView)
but then Use of unresolved identifier 'view'
.
My question is what am I doing wrong?
Thanks
EDIT:
So I have changed it to this now, is this correct?
import Foundation import UIKit
class MyViewController: UIViewController {
func background() {
var imageView : UIImageView
imageView = UIImageView(frame:CGRectMake(10, 50, 100, 300));
imageView.image = UIImage(named:"bg.png")
self.view.addSubview(imageView)
}
}
I no longer get any errors in that part but when calling the function from the viewcontroller class I get the error message Use of unresolved identifier 'background'. I should add that I am calling it in the other document by doing this
background()` in the view did load function.