I have learned how to rotate a view with Affine Transforms (see here). I have also learned about Auto Layout (see here and here), and even programmatic Auto Layout (see here and here). However, I don't know how to get Auto Layout to work with a rotated view.
This image shows what I would like to do:
I think the trouble comes from the width and the height changing because of the rotation. Is there any way to make a rotated view fill it's superview? Is there some trick to get Auto Layout to work or is it just incompatible after a rotation?
(I've only learned Swift, but I'd be glad to wade through Objective-C answers if that is what you are more familiar with.)
Update
Following @VinayJain's suggestion I did the following:
- pinned the subview edges to the superview in the storyboard.
created IBOutlets for the spacing constraints on all sides of the subview.
@IBOutlet weak var rightSpace: NSLayoutConstraint! @IBOutlet weak var leftSpace: NSLayoutConstraint! @IBOutlet weak var topSpace: NSLayoutConstraint! @IBOutlet weak var bottomSpace: NSLayoutConstraint!
rotated the subview
subview.transform = CGAffineTransformMakeRotation(CGFloat(M_PI_2))
changed the constraints from the outlets
self.rightSpace.constant = CGFloat(0) self.leftSpace.constant = CGFloat(0) self.topSpace.constant = CGFloat(0) self.bottomSpace.constant = CGFloat(0)
But it was at this point that I realized that I don't really need to change the spacing value. I want the spacing to remain 0. I just need it to adjust itself. However, the rotation messes that up. The effect is shown in the following image: