salipshitz salipshitz -4 years ago 85
Swift Question

Swift: Labels not showing up

I am trying to make labels defined by an array in Swift but when I try to do this and I run the app, they just don't show up. My code for the view controller is right here:import UIKit

class SmallPainViewController: UIViewController {

var tips = ["Play your favorite videogame", "Watch a movie", "Watch YouTube"]

override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
for (i, tip) in tips.enumerate() {
let tipLabel = UILabel(frame: CGRectMake(CGFloat(i*50+150), 0, 30, 30))
tipLabel.center = CGPointMake(CGFloat(i*50+150), 0)
tipLabel.text = tip
tipLabel.textAlignment = NSTextAlignment.Center
self.view.addSubview(tipLabel)
}
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}

}

Answer Source

Try to use:

var tips = ["Play your favorite videogame", "Watch a movie", "Watch YouTube"]

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
    for (i, tip) in tips.enumerated() {
        let tipLabel = UILabel(frame: CGRect(x: (i*50+150), y: 150, width: 30, height: 30))//CGRect(CGFloat(i*50+150), 0, 30, 30)
        tipLabel.center = CGPoint(x: (i*50+150), y: 150) //CGPoint(CGFloat(i*50+150), 0)
        tipLabel.text = tip
        tipLabel.textAlignment = NSTextAlignment.center
        self.view.addSubview(tipLabel)
    }
}

I think you are using swift 2.0

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download