1

I am trying to rotate a UILabel using this code,

self.itemlistLabel.text = "Playlist"
self.itemlistLabel.transform = CGAffineTransform(rotationAngle: -CGFloat.pi / 2)

This rotates the label, but this is not setting the label correctly inside the view, I have added a blue background colour to the label,

enter image description here

This is the screenshot from Xcode with the layouts set, but after applying transform this is not displaying as expected. How to fix this?

enter image description here

This is the final view, I am trying to make, enter image description here

4
  • What do you mean by "as expected"? What behavior do you expect? Commented Oct 1, 2020 at 17:00
  • Try to set self.itemlistLabel.layer.anchorPoint = CGPoint(x: 1, y: 1) or self.itemlistLabel.layer.anchorPoint = CGPoint(x: 0, y: 0) and check difference. Commented Oct 1, 2020 at 17:07
  • @ZGl6YXNt I tried both, but that shifted the label to left and the second one to top, I added the expected view. Thanks for your help. Commented Oct 1, 2020 at 17:26
  • 1) Apply transform to layer: self.itemlistLabel.layer.setAffineTransform(CGAffineTransform(rotationAngle: -CGFloat.pi / 2)) 2) Add proper constraints. You need to swap width and height: NSLayoutConstraint.activate([ self.itemlistLabel.leftAnchor.constraint(equalTo: labelSuperview.centerXAnchor), self.itemlistLabel.centerYAnchor.constraint(equalTo: labelSuperview.centerYAnchor), self.itemlistLabel.widthAnchor.constraint(equalTo: labelSuperview.heightAnchor), self.itemlistLabel.heightAnchor.constraint(equalTo: labelSuperview.widthAnchor), ]) Commented Oct 5, 2020 at 5:37

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.