我试图在 Xcode 6.1中以编程方式设置 UIImageView:
@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)
但是 XCode 说“预期声明”与 bgImage = UIImageView(image: image)
图像 "afternoon"
是一个 PNG,我的理解是 PNG 不需要在 XCode 6.1扩展。
也尝试仅 bgImage.image = UIImage(named: "afternoon")
,但仍然得到:
更新
好的,我已经将更新 UIImageView
的代码放入到 viewDidLoad
函数中,但是 UIImageView
仍然没有显示图像(在基本目录中的下午.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)
}