Alexander Leontev
Alexander Leontev

Reputation: 477

MPVolumeView animation on iOS 8

In iOS 8 there is a problem or a feature. When MPVolumeView is shown, it's being animated, like expanding from 0 to it's width. How can I fix that behavior? There was no such problem on iOS 7.

Upvotes: 8

Views: 1524

Answers (2)

Patrik
Patrik

Reputation: 1117

One possible way to remove this behavior is to subclass MPVolumeView and perform some additional work after [super layoutSubviews].

- (void)layoutSubviews
{
    [super layoutSubviews];

    [self cg_recursiveRemoveAnimationsOnView:self];
}

- (void)cg_recursiveRemoveAnimationsOnView:(UIView *)view
{
    [view.layer removeAllAnimations];
    for (UIView *subview in view.subviews) {
        [self cg_recursiveRemoveAnimationsOnView:subview];
    }
}

This removes all inserted animations. So be sure that is what you want, since this is quite the overkill. One could also just remove the position and bounds animations (see removeAnimationForKey:).

Upvotes: 2

tgebarowski
tgebarowski

Reputation: 1379

I confirm that this problem still exists in iOS 8. The workaround provided by Anastasia in one of the comments above (with overriding volumeSliderRectForBounds) seems to work but only when route button is not present. When it is present the slider overlaps the route button and it can no longer be pressed.

I made a simple modification of her solution, maybe someone can use it as a workaround, until Apple fixes that or better solution is provided.

- (CGRect)volumeSliderRectForBounds:(CGRect)bounds
{
    if (self.showsRouteButton) {
        NSInteger spacer = 10; /* Space between Route button and Volume slider */
        CGRect routeButtonRect = [self routeButtonRectForBounds:bounds];
        bounds.size.width -= (routeButtonRect.size.width + spacer);
    }
    return bounds;
}

I don't like hardcoding the spacer value, but I could not find how to calculate it dynamically.

Upvotes: 0

Related Questions