中心标签这些都是我与iOS在子视图
class ViewController: UIViewController {
// MARK: Properties
@IBOutlet weak var testLabel: UILabel!
// MARK: Events
override func viewDidLoad() {
super.viewDidLoad()
}
override func viewDidLayoutSubviews() {
testLabel.numberOfLines = 0
testLabel.sizeToFit()
testLabel.center.x = testLabel.superview!.center.x
//from here the label is centered
}
// MARK: Actions
@IBAction func trackButton(_ sender: UIButton) {
//first call: the label is aligned on the left
//following calls: the label is centered
testLabel.text = Track.createTrack().toText()
//even the following commented code does not allow the centering
//on the first call
//testLabel.sizeToFit()
//testLabel.center.x = testLabel.superview!.center.x
}
}
我拉我的头发了这一点。
============================================== ==============
更新项目
class ViewController: UIViewController {
// MARK: Properties
@IBOutlet weak var testLabel: UILabel!
// MARK: Events
override func viewDidLoad() {
super.viewDidLoad()
}
override func viewDidLayoutSubviews() {
//testLabel.numberOfLines = 0
//testLabel.sizeToFit()
//testLabel.center.x = testLabel.superview!.center.x
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
// MARK: Actions
@IBAction func trackButton(_ sender: UIButton) {
testLabel.text = Track.createTrack().toText()
}
}
您需要设置的限制界面生成器。 – rckoenes