I want to know how can I animate a change in the alignment property of an horizontal UIStackView say from UIStackViewAlignmentCenter to UIStackViewAlignmentTop and vice versa, and I want to visually reflects this change at the same time.
EDIT:
What I have:
This is my hierarchy of views:
- two buttons named b1 and b2; b1 has an action named "changeAlignmentToTop" and b2 has an action named "changeAlignmentToCenter", which are activated Upon touch Up Inside in the respective button.
- an UIView named Pview with frame (0,100,414,100).
- an horizontal UIStackView named S1, with the alignment property set as "center". This stackView has been pinned to the borders of pView.
- an horizontal UIStackView named S2, with the alignment property set as "Fill" and which has inside two labels.
- an horizontal UIStackView named S1, with the alignment property set as "center". This stackView has been pinned to the borders of pView.
What I want to do:
I want to change the alignment of S1 from "Center" to "Top" when I touch the button b1 and I want to animate that change.
What I tried
- (void) changeAlignmentToTop {
[UIView animationWithduration 0.3 animations : ^{
S1.alignment = UIStackViewAlignmentTop;
// here I'd prove several options
// option - 1
[S1 setNeedsDisplay];
[S1 layoutIfNeeded];
// option - 2
//[S1 setNeedsLayout];
//[S1 layoutIfNeeded];
// option - 3
//[S1 setNeedsDisplay];
//[pView layoutIfNeeded];
// option - 4
//[pView setNeedsDisplay];
//[pView layoutIfNeeded];
// option - 5
//[pView setNeedsDisplay];
//[self.view layoutIfNeeded];
} completion: nil];
}
- (void) changeAlignmentToCenter {
[UIView animationWithduration 0.3 animations : ^{
S1.alignment = UIStackViewAlignmentCenter;
// here I'd prove several options
// option - 1
[S1 setNeedsDisplay];
[S1 layoutIfNeeded];
// option - 2
//[S1 setNeedsLayout];
//[S1 layoutIfNeeded];
// option - 3
//[S1 setNeedsDisplay];
//[pView layoutIfNeeded];
// option - 4
//[pView setNeedsDisplay];
//[pView layoutIfNeeded];
// option - 5
//[pView setNeedsDisplay];
//[self.view layoutIfNeeded];
} completion: nil];
}
None of these options work. The aligment change but the UI does not reflects it neither it animates anything. If I change the alignment for axis (trying to change the axis from horizontal to vertical and vice versa) every things goes well. If I change animatedly the size of the font used in both label every thing goes well, the problem is with the alignment of the stackView. but according to the apple documentation the alignment property is animatable, so what I'm doing wrong??
Can anybody please help me??
Thanks in advance