5 ms·
Vary for traits doesn't do that, though! "Size of a particular view" isn't a trait, and "value of a single constraint" is different from the layout of the view
by panic 8y ago
Vary for traits doesn't do that, though! "Size of a particular view" isn't a trait, and "value of a single constraint" is different from the layout of the view.
Let me give a concrete example. Say you're writing a view to display a photo in a swipeable gallery. You want to position the associated UI elements where there happens to be room around the photo. That is, when the photo is tall, you want the UI to be placed on the side, but when the photo is wide, you'd prefer the UI to go on the bottom. The individual UI elements are mostly the same, but the layout is different for each state.
With -layoutSubviews, you can write your two layouts as usual, then use an if statement to switch between them:
- (void)layoutSubviews
{
if (_tallMode) {
// ... tall layout ...
} else {
// ... wide layout ...
}
}
Shared code can be factored out as necessary. Animation can be done with the following pattern:
[view layoutIfNeeded];
// -setTallMode: sets the layout needed flag if the value changed
[view setTallMode:shouldBeTall];
[UIView animateWithDuration:duration animations:^{
[view layoutIfNeeded];
}];
How would you specify these two states using IB/autolayout? And how would you animate between the states as you swipe between photos?
- bunnycorn 8y agoThat's not what you said in the first post, was it? If I wanted to do that, I would but the buttons with two constraints to the bottom and to the trailing (right), and I would change the constants of those two constraints, it's even easy because it's just swapping the constants of the constraint pair.