2017-06-12 186 views
0

我想在Swift中使數組定義標籤,但是當我嘗試這樣做並且運行該應用程序時,它們只是不顯示。我的視圖控制器代碼就在這裏:進口UIKit的Swift:標籤沒有顯示

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

'tipLabel.center = CGPointMake(CGFloat的(我* 50 + 150),0)',爲什麼?你沒有足夠的設置框架?此外,你的意思是'tipLabel.center = CGPointMake(CGFloat(i * 50 + 150),15)' – Larme

回答

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

+0

我實際上使用2.1 – salipshitz

+0

上面的代碼與Swift 3.1完美配合 – agscastaneda

+0

我不得不稍微修改它以去我的Swift版本 – salipshitz