I'm trying to set UIImageView programmatically in Xcode 6.1:
@IBOutlet weak var bgImage: UIImageView!
var image : UIImage = UIImage(named:"afternoon")!
bgImage = UIImageView(image: image)
bgImage.frame = CGRect(x: 0, y: 0, width: 100, height: 200)
view.addSubview(bgImage)
But Xcode is saying "expected declaration" with bgImage = UIImageView(image: image)
Image "afternoon"
is a PNG, and my understanding is PNG does not need an extension in XCode 6.1.
Also tried just bgImage.image = UIImage(named: "afternoon")
, but still get:
UPDATE
OK, I have put the code to update UIImageView
into the viewDidLoad
function, but UIImageView
is still not showing the image (which exists in the base directory as afternoon.png):
@IBOutlet weak var bgImage: UIImageView!
@IBOutlet weak var dateLabel: UILabel!
@IBOutlet weak var timeLabel: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
updateTime()
var timer = NSTimer()
let aSelector : Selector = "updateTime"
timer = NSTimer.scheduledTimerWithTimeInterval(0.01, target: self, selector: aSelector, userInfo: nil, repeats: true)
var image : UIImage = UIImage(named:"afternoon")!
bgImage = UIImageView(image: image)
}
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…