The content of UILabel is being truncated after rotation in Swift 4

425 views Asked by At

I am using AutoLayout. I have a view with %10 width and 200 fixed height. I placed a UILabel into that view with 0 margins. Then I am rotating the UILabel, changing the content and calling sizeToFit() function. At the load of the controller the rotation works but the content is being truncated. After I did a button click on the controller, the UILabel is being enlarged and content is being fit.

What should I do to avoid this problem?

Note: I checked the width and height of the label's frame with debugging. After sizeToFit() the width and height of the frame changes as expected. However, the UI is not refreshing. I also tried label.setNeedsLayout() and label.layoutIfNeeded() but nothing changed.

For rotation I am using below extension:

extension UILabel {
    @IBInspectable
    var rotation: Int {
        get {
             return 0
        } set {
            let radians = CGFloat(CGFloat(Double.pi) * CGFloat(newValue) / CGFloat(180.0))
            self.transform = CGAffineTransform(rotationAngle: radians)
        }
    }
}
1

There are 1 answers

0
Andrey Volobuev On

You can try view.layoutIfNeeded() instead of label.layoutIfNeeded()