Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I rotate an UIImageView in Interface Builder?

Is it possible to rotate an UIImageView in Interface Builder from XCode? Like I can do it in Photoshop, where I can scale and rotate an image.

I know it is possible by code but is there a way to do this in the interface builder?

like image 933
gpichler Avatar asked Feb 01 '13 20:02

gpichler


People also ask

How do I rotate UIImageView?

Step 1 − Open Xcode→SingleViewApplication→name it RotateImage. Step 2 − Open Main. storyboard, add UIImageView and add 2 buttons as shown below name them ROTATE BY 90 DEGREES AND ROTATE BY 45 DEGREES.

How do you rotate a view in Swift?

rorateview.swiftlet bitmap: CGContext = UIGraphicsGetCurrentContext()! //Move the origin to the middle of the image so we will rotate and scale around the center. let newImage: UIImage = UIGraphicsGetImageFromCurrentImageContext()!


2 Answers

Yes, you can do this in interface builder without any additional code with layer.transform.rotation.z runtime attribute. Note that this value is in radians.

rotation

like image 161
Leszek Szary Avatar answered Sep 18 '22 16:09

Leszek Szary


Yes you can but using a little trick.

Declare in your code a new UIView Category like this

@interface UIView (IBRotateView)

@property (nonatomic, assign) CGFloat rotation;

@end


@implementation UIView (IBRotateView)
@dynamic rotation;

- (void)setRotation:(CGFloat)deg
{
    CGFloat rad = M_PI * deg / 180.0;

    CGAffineTransform rot = CGAffineTransformMakeRotation(rad);

    [self setTransform:rot];
}

@end

Now you can use a runtime parameter "rotation" directly on interface builder like this on any UIView you like.

enter image description here

Obviously interface builder will not rotate the view in its internal render because it will only effect the runtime rendering.

like image 30
Gabriele Avatar answered Sep 19 '22 16:09

Gabriele