Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CGAffineTransformMakeRotation and Autolayout issues under iOS8

After applying a rotation transformation on a simple UIView

CGAffineTransform trans = CGAffineTransformMakeRotation(M_PI * -0.5);

simpleVIew_.transform = trans;

Which has the following constraints

[self addConstraints:@[
   [NSLayoutConstraint constraintWithItem: simpleView_ attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual
                                   toItem:nil attribute:NSLayoutAttributeNotAnAttribute
                               multiplier:1 constant:50],

   [NSLayoutConstraint constraintWithItem: simpleView_ attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual
                                   toItem:self attribute:NSLayoutAttributeHeight
                               multiplier:1 constant:270],

   [NSLayoutConstraint constraintWithItem: simpleView_ attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual
                                   toItem:self attribute:NSLayoutAttributeLeft
                               multiplier:1.0 constant:0],

   [NSLayoutConstraint constraintWithItem: simpleView_ attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual
                                   toItem:self attribute:NSLayoutAttributeBottom
                               multiplier:1.0 constant:0]

]];

I get two different results between iOS7.1 and iOS8 beta 5:

iOS7.1

<UIView: 0x1655d990; frame = (0 30; 50 270); transform = [0, -1, 1, 0, 0, 0]; opaque = NO; layer = <CALayer: 0x1655dab0>

iOS8 beta 5

<UIView: 0x16e5a530; frame = (-110 140; 270 50); transform = [0, -1, 1, 0, 0, 0]; opaque = NO; layer = <CALayer: 0x16e61880>>

Notice the differences in the frame - the width/height values have switched and the x,y coordinates have changed.

Any ideas why there's such a huge difference between 7.1 and 8?

like image 468
Shai Avatar asked Aug 12 '14 10:08

Shai


1 Answers

Not really a solution but more of a work around.

I have found that top, bottom, left, and right constraints behave differently when building on iOS 8 vs iOS 7 when using CGAffineTransformMakeRotation. However, CenterX and CenterY behave the same for each. Also, height and width constraints will behave the same as long as you addConstraint to the same view you constraintWithItem.

So, if you can change your constraints for a view that uses CGAffineTransformMakeRotation to only use height, width, CenterX, CenterY, then you should be able to have the same constraints appear the same when building on both iOS versions.

Below is an exert from my own code, which was giving me the same layout regardless of iOS version:

// Rotate View 90º
endorseHereLabel.transform = CGAffineTransformMakeRotation(-(M_PI)/2);

[self addSubview:endorseHereLabel];
[self bringSubviewToFront:endorseHereLabel];

// Configure Constraints
[self addConstraint:[NSLayoutConstraint constraintWithItem:endorseHereLabel
                                                 attribute:NSLayoutAttributeCenterY
                                                 relatedBy:NSLayoutRelationEqual
                                                    toItem:lineView1
                                                 attribute:NSLayoutAttributeCenterY
                                                multiplier:1.0
                                                  constant:40.0]];
[self addConstraint:[NSLayoutConstraint constraintWithItem:endorseHereLabel
                                                 attribute:NSLayoutAttributeCenterX
                                                 relatedBy:NSLayoutRelationEqual
                                                    toItem:lineView1
                                                 attribute:NSLayoutAttributeCenterX
                                                multiplier:1.0
                                                  constant:kBottomBuffer]];
[endorseHereLabel addConstraint:[NSLayoutConstraint constraintWithItem:endorseHereLabel
                                                             attribute:NSLayoutAttributeHeight
                                                             relatedBy:NSLayoutRelationEqual
                                                                toItem:nil
                                                             attribute:NSLayoutAttributeNotAnAttribute
                                                            multiplier:1.0
                                                              constant:12.0]];
[endorseHereLabel addConstraint:[NSLayoutConstraint constraintWithItem:endorseHereLabel
                                                             attribute:NSLayoutAttributeWidth
                                                             relatedBy:NSLayoutRelationEqual
                                                                toItem:nil
                                                             attribute:NSLayoutAttributeNotAnAttribute
                                                            multiplier:1.0
                                                              constant:250.0]];
like image 198
bmjohns Avatar answered Oct 23 '22 19:10

bmjohns