это мои первые шаги с прошивкойцентрирования этикетки в подвид
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