斯威夫特:标签没有出现

时间:2017-06-12 22:20:35

标签: arrays swift swift2 uilabel

我正在尝试在Swift中使用数组定义标签,但是当我尝试执行此操作并运行应用程序时,它们只是不显示。我的视图控制器代码就在这里: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.
    }

}

1 个答案:

答案 0 :(得分:1)

尝试使用:

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)
    }
}

我认为你正在使用swift 2.0