Xcode 10, Swift 5, iOS 12
I've got two UILabels in a horizontal StackView:
- StackView: Alignment Fill & Distribution Fill
- Label 1: No special constraints
- Label 2: Proportional Width (half the size of Label 1)
Label 2 contains a word that's too long for the label size on an iPhone SE (smallest supported device), so I'm using an abbreviated version. On a bigger device, e.g. an iPad, I want to display the full word (should only be set once), so I tried this:
var label2set:Bool = false
print("label2: \(label2.frame.width)")
if !label2set && label2.frame.width > 100 && (UIApplication.shared.statusBarOrientation == .portrait || UIApplication.shared.statusBarOrientation == .portraitUpsideDown) {
label2set = true
label2.text = "VeryLongLabelText"
}
No matter if I use this code in viewDidLoad or viewWillAppear, the first time it's called the supposed label width is only about 70 (even on an iPad), even though it's clearly bigger in the simulator.
If I put the code in viewWillAppear and remove the check for label2set, then switch to the next view through my NavigationController and go back to my original one again, the code is called properly and the label displays the full text (width: about 200 on an iPad).
When do labels actually get set to their proper width, so when and how can I check the size?