Custom MPVolumeView Thumb Image not vertically centered since iOS 5.1

I'm building an application that needs an MPVolumeView to control the volume. It worked perfectly before iOS 5.1 but since the 5.1 update the thumb image is no longer vertically centered. I tried a few things like changing imagine dimensions, resizing my views (and slider) but nothing seems to work, the thumb is just not vertically centered anymore. The only way i get a centered thumb is if i use the default iOS one.

I tried adding a UISlider to another view with the exact min, max and thumb image and that one is centered fine.

Here is the code for the MPVolumeView:

MPVolumeView *volumeView;
volumeView = [[[MPVolumeView alloc] initWithFrame:volumeViewHolder.bounds] autorelease];
[volumeViewHolder addSubview:volumeView];

UIView  *volumeViewSlider;
for (UIView *view in [volumeView subviews])
{
    if ([[[view class] description] isEqualToString:@"MPVolumeSlider"])
    {
        volumeViewSlider = view;
    }
}

[(UISlider *)volumeViewSlider setThumbImage:sliderHandleIcon forState:UIControlStateNormal];
    [(UISlider *)volumeViewSlider setMinimumTrackImage:leftTrackImage forState:UIControlStateNormal];
    [(UISlider *)volumeViewSlider setMaximumTrackImage:rightTrackImage forState:UIControlStateNormal];

volumeViewHolder is just a UIView thats 153x33. I put the thumb in green in the screenshot. 在这里输入图像描述


Maybe a better solution:

User a bigger image with a transparent border on the bottom. Should be around 10px for Retina Displays.


the same problem i resolved in one project. Must be set color of left part and right part with alpha = 0 -it means transparent all slider without thumb (without moovable part of it). After we must create custom view for line of slider, without thumb. In this view any colored part may be shifted as you want, upper or below, left or right. It obtained using the defined y for your ocassion:

UIView *v = [[UIView alloc] initWithFrame:CGRectMake(x,y,width, height)];

And add the slider to this line as subview. Resulted view will be slider. For example:

UISlider *ourSlider  = ...; 
//initialise UISlider

ourSlider.minimumTrackTintColor = [UIColor colorWithRed:0 green:122.0f/255.0f blue:1 alpha:0];

ourSlider.minimumTrackTintColor = [UIColor colorWithRed:0 green:122.0f/255.0f blue:1 alpha:0];

UIView *lineOfSliderWithoutThumb = ... ;
// creation it

[lineOfSliderWithoutThumb addSubview:ourSlider];

//after this lineOfSliderWithoutThumb is the our custom uislider. Note: colors there are used as default slider colors of left and right sides of UISlider.

链接地址: http://www.djcxy.com/p/61020.html

上一篇: 友好的数字+空格字符串输入(信用卡号码)

下一篇: 自iOS 5.1以来,自定义MPVolumeView Thumb Image不是垂直居中