在Swift的操場上,我如何將UILabel
置於UIViewController
的內部?如何在Swift中居中文本?
當我使用title.center = CGPoint(x: vc.view.frame.width/2, y: 20)
時,文本會熄滅。
這是問題
如果有幫助,這是我的代碼。
import UIKit
import PlaygroundSupport
// Set up ViewController and other things
var vc = UIViewController()
vc.view.frame.size.height = 75
vc.view.backgroundColor = .white
let title = UILabel(frame: CGRect(x: 0, y: 0, width: 200, height: 50))
title.textAlignment = .center
title.center = vc.view.center
title.text = "Which code is correct?"
vc.view.addSubview(title)
PlaygroundPage.current.liveView = vc
嘗試title.center = self.view.center –
@Harambe隊http://stackoverflow.com/questions/ 34645943/how-to-center-uilabel-in-swift –
title.center = GCPoint(x:self.view.bounds.size.width/2.0,y:self.view.bounds.size.height/2.0) –