如何自定义MPVolumeView? [英] How to customize MPVolumeView?

查看:148
本文介绍了如何自定义MPVolumeView?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经尝试了很多方法来实现常规的 UISlider 并控制设备卷,但它是所有Native-C函数,导致许多无法追踪的错误。

I have tried many methods to implement a regular UISlider and control the device volume, but it's all Native-C functions which results in many untraceable bugs.

我尝试了 MPVolumeView 它就像魅力一样,甚至在关闭应用程序后甚至可以控制设备音量,就像iPod应用程序一样。

I tried the MPVolumeView it works like charm, it even controls the device volume even after you close the app, just like the iPod app.

我的问题是,无论如何要定制具有特定颜色和图像的 MPVolumeView ,就像 UISlider

My question is, is there anyway to customize the MPVolumeView with specific colors and images, just like UISlider?

注意:我想要一个合法的方法而不使用私有的未记录的API。

NOTE: I want a legal method without using private undocumented APIs.

UPDATE

根据@Alexsander Akers的回答,因为子视图隐藏在 MPVolumeView 我不得不循环浏览子视图,获取 UISlider 并自定义它,这是代码。

UPDATE
As per @Alexsander Akers answer, since the sub views are hidden in MPVolumeView I had to cycle through subviews, get the UISlider and customize it, here is the code.

IBOutlet UISlider *volumeSlider;   //defined in <class.h> and connected to a UISlider in Interface Builder

-(void) viewDidLoad {
     ....
     [self setCustomSlider];
     ....
}

-(void) setCustomSlider{
     MPVolumeView *volumeView = [[[MPVolumeView alloc] initWithFrame:[volumeSlider frame]] autorelease];
     NSArray *tempArray = volumeView.subviews;

     for (id current in tempArray){
           if ([current isKindOfClass:[UISlider class]]){
                    UISlider *tempSlider = (UISlider *) current;
                    UIImage *img = [UIImage imageNamed:@"trackImage.png"];
                    img = [img stretchableImageWithLeftCapWidth:5.0 topCapHeight:0];
                    [tempSlider setMinimumTrackImage:img forState:UIControlStateNormal];

                    [tempSlider setThumbImage:[UIImage imageNamed:@"thumbImage.png"] forState:UIControlStateNormal];

           } 
    }
    [volumeSlider removeFromSuperview];
    [self.view addSubview:volumeView];
 }


推荐答案

你可以尝试骑自行车子视图并查找UISlider子类?

You could try cycling through its subviews and look for a UISlider subclass?

这篇关于如何自定义MPVolumeView?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆