I\'m trying to rotate an UIView
a few radians but after applying the transformation it doesn\'t look to be keeping its size. What\'s the proper way to achieve t
You're probably hitting a problem with Autolayout. You probably have constraints on the rotated view pinning it to the edges of the superview. When the transform is applied, Autolayout is updating the view's size to still fit within the superview.
You can experiment with different constraints (e.g. pinning the centre of the view to the centre of another view, and pinning the width and height to constant values) or turn Autolayout off for the rotated view, or, if these don't work or don't suit your needs, use a container view which is laid out under Autolayout, and add your rotating view to this, without using Autolayout.
This can only be done in code - you can make individual views subject to Autolayout or not by setting translatesAutoresizingMasksIntoConstraints
to NO
(Autolayout on) or YES
(Autolayout off). You'll need to set the appropriate autoresizing masks if you switch a view from one to the other.
Swift5
Rotate UIView Upside down
let degrees:CGFloat = -180 //angle to convert upside down
rotatingUI.transform = CGAffineTransform(rotationAngle: degrees * CGFloat(Double.pi)/180);
Swift 5:
extension UIView {
func setTransformRotation(toDegrees angleInDegrees: CGFloat) {
let angleInRadians = angleInDegrees / 180.0 * CGFloat.pi
let rotation = self.transform.rotated(by: angleInRadians)
self.transform = rotation
}
}
Try with this code:
#define DEGREES_TO_RADIANS(angle) ((angle) / 180.0 * M_PI)
double rads = DEGREES_TO_RADIANS(240);
self.arrowView.layer.transform = CATransform3DMakeRotation(rads, 0, 0, 1);