Custom MPVolumeView Thumb Image not vertically centered since iOS 5.1

会有一股神秘感。 提交于 2020-01-01 09:18:14

问题


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.


回答1:


Maybe a better solution:

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




回答2:


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.



来源:https://stackoverflow.com/questions/11230552/custom-mpvolumeview-thumb-image-not-vertically-centered-since-ios-5-1

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!