Changing UIView rotation will change it's frame size. How to keep frame size after rotating?


I'm rotating UIView by the UIRotationGestureRecognizer but I have a problem, The rotated UIView's frame size will be changed on rotation.

targetView = UIView(frame: CGRect(x: self.view.center.x, y: self.view.center.y , width: 100, height: 100)) targetView.backgroundColor = .red targetView.isUserInteractionEnabled = true self.view.addSubview(targetView) targetView.center = self.view.center let rotate = UIRotationGestureRecognizer(target: self, action: #selector(rotatedView)) targetView.addGestureRecognizer(rotate)

Here is the rotate function:

@objc func rotatedView(_ sender: UIRotationGestureRecognizer) { var originalRotation = CGFloat() if sender.state == .began { sender.rotation = lastRotation originalRotation = sender.rotation print("TargetView Frame Size: [START ROTATE] ", self.targetView.frame.size) print("TargetView Bounse Size: [START ROTATE] ", self.targetView.bounds) } else if sender.state == .changed { let newRotation = sender.rotation + originalRotation sender.view?.transform = CGAffineTransform(rotationAngle: newRotation) print("TargetView Frame Size: [ROTATETING] ", self.targetView.frame.size) print("TargetView Bounse Size: [ROTATING ROTATE] ", self.targetView.bounds) } }

Size will be changed:

TargetView Frame Size: [START ROTATE] (100.0, 100.0) TargetView Frame Size: [ROTATETING] (110.564206726133, 110.564206726133)

How to keep frame size after rotating just like Start Rotating?!


I have these gestures together: Rotate, Resize, Move.

Move is working well, but Rotate is changing size. I can use bounds here but If I resize too, the bounds is not useful anymore.


You need the scaled but non-rotated view size. You can compute that from the bounds.size of the view and the transform.


let view = UIView(frame: CGRect(x: 0, y: 0, width: 100, height: 150)) // Scale view by 3X horizontally and by 4X vertically and // rotate by 45 degrees view.transform = CGAffineTransform(scaleX: 3, y: 4).rotated(by: 45.0 / 180.0 * .pi) let transform = view.transform // Find the angle, print it in degrees let angle = atan2(-transform.c, transform.a) print(angle * 180.0 / .pi) // 44.99999999999999 // Find scaleX and scaleY let scaleX = transform.a * cos(angle) - transform.c * sin(angle) let scaleY = transform.d * cos(angle) + transform.b * sin(angle) print(scaleX) // 3.0 print(scaleY) // 4.0 print(view.frame.size) // (530.3300858899106, 707.1067811865476) print(view.bounds.size) // (100.0, 150.0) let adjustedSize = CGSize(width: view.bounds.size.width * scaleX, height: view.bounds.size.height * scaleY) // print non-rotated but scaled size print(adjustedSize) // (300.0, 600.0) <hr />

Here it is as a handy extension that will work for views that are scaled, rotated, and translated (moved):

extension CGAffineTransform { var angle: CGFloat { return atan2(-self.c, self.a) } var angleInDegrees: CGFloat { return self.angle * 180 / .pi } var scaleX: CGFloat { let angle = self.angle return self.a * cos(angle) - self.c * sin(angle) } var scaleY: CGFloat { let angle = self.angle return self.d * cos(angle) + self.b * sin(angle) } }

Now, if you have a UIView:

let angle = view.transform.angleInDegrees let scaleX = view.transform.scaleX let scaleY = view.transform.scaleY let adjustedSize = CGSize(width: view.bounds.size.width * scaleX, height: view.bounds.size.height * scaleY)


  • Entity Framework with Instead Of triggers
  • Convert a datetime string to millisecond UNIX time stamp
  • Method design - clarity or multifunction
  • How to reinstall Ruby and Gem on Mac OS X
  • how to make pdf page to fit to screen size in vfr reader
  • Mathematical Equality for Func
  • NDB using Users API to form an entity group
  • How do I know if text exceeds the bounds of a text view?
  • Warning: the current language does not match this frame
  • disabling ARC for .h files iphonesdk
  • how to specify height of row?
  • Update the target coordinates while transitioning
  • How to move cursor from one text field to another automatically when the user clicks the Delete Butt
  • Cocos2D set sprite position in relation to another sprite
  • Adjust UILabel Dynamically Within UITableViewCell?
  • Responsive left sidebar open close
  • How Get arguments value using inline assembly in C without Glibc?
  • How to make R's read_csv2() recognise the text characters properly
  • Implementation of State Monad
  • How do I pass the string value parameter of the selected list item from an auto-populated dropdown l
  • Fill an image in a square container while keeping aspect ratio
  • Where to put my custom functions in Wordpress?
  • Can I have the cursor start on a particular column by default in jqgrid's edit mode?
  • 'TypeError' while using NSGA2 to solve Multi-objective prob. from pyopt-sparse in OpenMDAO
  • How to limit post in wp_query
  • Websockets service method fails during R startup
  • Return words with double consecutive letters
  • bootstrap to use multiple ng-app
  • Comma separated Values
  • How to delete a row from a dynamic generate table using jquery?
  • How to set the response of a form post action to a iframe source?
  • python draw pie shapes with colour filled
  • Are Kotlin's Float, Int etc optimised to built-in types in the JVM? [duplicate]
  • Running Map reduces the dimensions of the matrices
  • Reading document lines to the user (python)
  • Binding checkboxes to object values in AngularJs
  • Python/Django TangoWithDjango Models and Databases
  • Net Present Value in Excel for Grouped Recurring CF
  • jQuery Masonry / Isotope and fluid images: Momentary overlap on window resize
  • How to load view controller without button in storyboard?