iOS 7 - 自定义UISlider设计无法正常工作

时间:2013-09-16 05:36:35

标签: iphone ios uislider ios7

在iOS的早期版本中,一切正常,但在iOS 7上没有。 它没有为UISlider设置自定义设计,因此视图上没有任何内容,“Volume”滑块视图为空。其他一切都很好。

代码:

for (UISlider *slider in volumeSlider.subviews) {
        NSLog(@"Searching!");
        if ([slider isKindOfClass:[UISlider class]]) {
            NSLog(@"SLIDER FOUND!");
            [slider setThumbImage:[UIImage imageNamed:@"thumbSlider.png"] forState:UIControlStateNormal];
            [slider setMinimumTrackImage:[[UIImage imageNamed:@"sliderMax.png"] stretchableImageWithLeftCapWidth:10.0 topCapHeight:0.0] forState:UIControlStateNormal ];
            [slider setMaximumTrackImage:[[UIImage imageNamed:@"sliderMin.png"] stretchableImageWithLeftCapWidth:10.0 topCapHeight:0.0] forState:UIControlStateNormal ];
        }
    }

2 个答案:

答案 0 :(得分:5)

iOS7现已发布,因此NDA不再适用。如果您需要支持低于iOS6的操作系统,您可以使用此代码。这也适用于iOS7。注意:如果您只需要支持iOS6及更高版本,Apple提供直接API来设置这些图像以自定义MPVolumeView的外观,特别是setVolumeThumbImage:(UIImage *)forState:(UIControlState),setMinimumVolumeSliderImage:(UIImage *)forState :(UIControlState)和setMaximumVolumeSliderImage:(UIImage *)forState:(UIControlState)。

// replace the standard look and feel for the MPVolumeSlider with custom images
// note: on iOS7 it is necessary to set the thumb image last or the thumb will be drawn under the slider (not pretty), on iOS6 the order didn't seem to matter.
UIView *a = nil;
for (UIView *view in [self.volumeView subviews]) {
    if ([[[view class] description] isEqualToString:@"MPVolumeSlider"]) {
        a=view;
        UIImage *volumeBackgroundImage = [[UIImage imageNamed:@"volume_background"] resizableImageWithCapInsets:UIEdgeInsetsMake(9, 5, 7, 5)];
        [(UISlider *)a setMinimumTrackImage:[[UIImage imageNamed:[[ApplicationType sharedInstance] imageForAppType:@"volume_progress"]] resizableImageWithCapInsets:UIEdgeInsetsMake(9, 5, 7, 5)]  forState:UIControlStateNormal];
        [(UISlider *)a setMaximumTrackImage:volumeBackgroundImage forState:UIControlStateNormal];
        [(UISlider *)a setThumbImage:[UIImage imageNamed:@"volume_handle"] forState:UIControlStateNormal];
    }
}

答案 1 :(得分:0)

可能Apple改变了一些API。如果您将此代码用于电影播放器​​的音量滑块,请尝试使用MPVolumeView