Reputation: 37
I am struggling with Autolayout (using it for the first time in a new app).
Following situation:
In one Popover I need to re-layout 8 buttons programmatically according to user-selection. This works fine until the moment where the buttons must be rotated by 180° (to be viewed upside down, which is an app feature).
As soon as I add the code for the rotation, the relayout does not work anymore. I don't understand why I can change the position of the buttons (like Case 2), but then, when rotating it doesnt work anymore.
Case 1:
BUTTON 0 | BUTTON 1 | BUTTON 2 | BUTTON 3
BUTTON 4 | BUTTON 5 | BUTTON 6 | BUTTON 7
Case 2:
BUTTON 7 | BUTTON 6 | BUTTON 5 | BUTTON 4
BUTTON 3 | BUTTON 2 | BUTTON 1 | BUTTON 1
(all buttons rotated 180°, i.e. flipped upside down)
Once AutoLayout is switched off, it works like a charm. The problem is that as soon as I turn off AutoLayout the rest of the App is completely screwed up.
It would be great if somebody could help me understand this problem.
The code is in viewDidAppear
for (UIButton *button in self.allButtons) {
if (button.tag == i) {
button.transform = CGAffineTransformMakeRotation(M_PI);
button.frame = CGRectMake((300 - (100 * currentButtonPosition)) + offsetWidthButton, offsetHeightButton, 100, 100);
NSLog(@"Button Tag Nr: %d\nFrame:\nX=%f\nY=%f\nW=%f\nH=%f",button.tag, button.frame.origin.x, button.frame.origin.y, button.frame.size.width, button.frame.size.height);
}
}
The frame coordinates for the buttons are all correct in the NSLog output. I guess somehow the button.transform screws up the .frame = CGRectMake part, but why I don't understand and am looking forward for your help or guidance where to continue to study the mysterious autoLayout.
Hope the explanation is understandable :-)
Thanks and have a nice day Ronnie
Upvotes: 1
Views: 2013
Reputation: 2490
Bug or unexpected behaviour is a bit improved in last Xcode version, but it is still messy. For example, you still can't switch off AutoLayout on one XIB independently without consequences, although the checkboxes on others are not changed when changed on single one.
What you want to do is following:
before switching off the AUto Layout, set frames according to constraints in all the views, delete all constraints within all the views in the application and then turn off auto layouts.
This is of course much less painful, of you have set constraints in IB and not programatically.
Auto Layouts are unfortunately still well behind expected functionality, they especially lacks in defining and recognizing intrinsic positions and even fas worse in automatically setting constraints. It's a vast area for improvements.
Upvotes: 1
Reputation: 11672
If you're using autolayout you should be changing the constraints, not setting the button's frame.
Upvotes: 1