MPVolumeView在iOS 7中无法正确重绘

jrw*_*agz 8 uiview ios mpvolumeview ios7

所以我有一个iOS7应用程序,我正在使用MPVolumeView,所以用户可以控制音量级别.我有一个隐藏在此特定位置的路线按钮,MPVolumeView并使用另一个MPVolumeView禁用滑块作为我的AirPlay图标.

我的应用程序支持纵向和横向,并且在这两种不同模式下,音量滑块的宽度不同.

如果视图首先在横向模式下初始化,那么MPVolumeView它将正确调整其大小.

但是,当视图在纵向模式下初始化然后我旋转到横向模式时,应用程序中的所有其他内容都会被调整大小/移动,除了MPVolumeView唯一的移动,并且它不会像它应该的那样变短.

我正在使用自定义图像MPVolumeView,如果我删除了轨道的自定义图像,这个问题就消失了.

这是用于初始化的代码 MPVolumeView

    self.volumeView = [[MPVolumeView alloc] initWithFrame:CGRectZero];
    self.volumeView.showsRouteButton = NO;
    self.volumeView.layer.borderColor = [[UIColor redColor] CGColor];
    self.volumeView.layer.borderWidth = 1.0f;
    if (AT_LEAST_IOS7) {
        // TODO: BUGBUG iOS7 doesn't redraw this MPVolumeView correctly when it's frame changes (i.e. we rotate to the portrait view).
        // These images simply make the bar a little thicker than the standard thickness (to match the iOS7 music app) but it's not redrawing
        // correctly so we are going to have to live with a slightly thinner bar.
        [self.volumeView setMaximumVolumeSliderImage:[UIImage imageNamed:@"volume_bar_max"] forState:UIControlStateNormal];
        [self.volumeView setMinimumVolumeSliderImage:[UIImage imageNamed:@"volume_bar_min"] forState:UIControlStateNormal];
        [self.volumeView setVolumeThumbImage:[UIImage imageNamed:@"volume_scrubber"] forState:UIControlStateNormal];
    }
    [self addSubview:self.volumeView];
Run Code Online (Sandbox Code Playgroud)

在layoutSubviews中,我正在重新定位/缩放它的框架:

self.volumeView.frame = CGRectIntegral(CGRectMake(controlsLeft + kEdgeToSliderSideWidth,
                                                  volumeTop,
                                                  controlsWidth - (2 * kEdgeToSliderSideWidth),
                                                  volumeSize.height));
Run Code Online (Sandbox Code Playgroud)

以下是在纵向模式下视图开始时的样子:(总宽度为640像素)

肖像模式

当它旋转到横向时,它看起来像这样:(总宽度为568像素)

景观模式

有人有什么想法吗?

小智 5

我遇到了同样的问题。我目前的解决方法是在屏幕旋转后销毁并重新添加滑块:

- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
{
    //remove the old view
    for (UIView *subview in self.volumeViewContainer.subviews)
        [subview removeFromSuperview];

    //recreate it
    UIImage *slider = [[UIImage imageNamed:@"slider"] resizableImageWithCapInsets:UIEdgeInsetsMake(0.0, 15.0, 0.0, 15.0)];
    UIImage *knobImage = [UIImage imageNamed:@"slider_knob"];
    MPVolumeView *volumeView = [[[MPVolumeView alloc] initWithFrame:self.volumeViewContainer.bounds] autorelease];
    [volumeView setMinimumVolumeSliderImage:slider forState:UIControlStateNormal];
    [volumeView setMaximumVolumeSliderImage:slider forState:UIControlStateNormal];
    [volumeView setVolumeThumbImage:knobImage forState:UIControlStateNormal];
    [self.volumeViewContainer addSubview:volumeView];
}
Run Code Online (Sandbox Code Playgroud)

这样做仍然存在滑块在旋转期间仍然显示毛刺的问题。这就是为什么我将滑块渲染为图像并在旋转发生之前将滑块与所述图像交换:

- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
    //render to UIImage
    UIGraphicsBeginImageContextWithOptions(self.volumeViewContainer.frame.size, NO, 0.0);
    [self.volumeViewContainer.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage * img = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    //remove old slider
    for (UIView *subview in self.volumeViewContainer.subviews)
        [subview removeFromSuperview];

    //add UIImageView which resizes nicely with the container view
    UIImageView *imageView = [[[UIImageView alloc] initWithFrame:self.volumeViewContainer.bounds] autorelease];
    imageView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
    imageView.image = img;
    [self.volumeViewContainer addSubview:imageView];
}
Run Code Online (Sandbox Code Playgroud)

我知道这不是一个真正的解决方案,但我认为它总比没有好,我希望它对您有所帮助。