更改MPVolumeView路线按钮iOS 7的颜色

时间:2013-09-29 07:04:14

标签: ios ios7 mpvolumeview

我正在为iOS 7设计一个音乐应用程序,我希望将“AirPlay”路由选择器按钮直接放在我的应用程序中。我可以将按钮放置得很好,但是它没有显示,因为图标是白色的,我的背景是白色的。

有没有办法改变路线按钮的颜色?

以下是我用来创建按钮的代码。

self.airPlayButton = [[MPVolumeView alloc] initWithFrame:CGRectZero];
    self.airPlayButton.showsVolumeSlider = NO;
    [self.airPlayButton sizeToFit];
    self.airPlayButton.backgroundColor = [UIColor myGreenColor];
    [self addSubview:self.airPlayButton];

基本上下面的图片是我想要的,除了我想要的图标是绿色而不是它的背景。

MPVolumeView Route Button with background changed

3 个答案:

答案 0 :(得分:8)

在回顾亚当斯的回答时,我更喜欢这项任务。所以我稍微保护了那段代码:

目标-C:

for( UIView *wnd in volumeView.subviews ) {
    if( [wnd isKindOfClass:[UIButton class] ]) {
        UIButton *button = (UIButton*) wnd;
        UIImage *img = button.currentImage;
        UIImage *img2 = [img imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
        [volumeView setRouteButtonImage: img2 forState:UIControlStateNormal];
        break;
    }
}

夫特:

    for view in volumeView.subviews {
        if view.isKindOfClass(UIButton) {
            let buttonOnVolumeView : UIButton = view as! UIButton
            volumeView.setRouteButtonImage(buttonOnVolumeView.currentImage?.imageWithRenderingMode(.AlwaysTemplate), forState: .Normal)
            break;
        }
    }

现在它对volumeView的tintColor属性做出反应,如果Apple决定添加另一个按钮或更改序列,则此代码仍然有效。

答案 1 :(得分:6)

创建您自己的图片并尝试     setRouteButtonImage:forState:     将按钮图像指定给指定的控件状态。

- (void)setRouteButtonImage:(UIImage *)image forState:(UIControlState)state
Parameters
image
The image to associate with the specified states.
state
The control state with which to associate the image.
Discussion
Use this to customize the appearance of the route button when it is enabled, disabled, highlighted, and so on.

Availability
Available in iOS 6.0 and later.
See Also
– routeButtonImageForState:
Declared In
MPVolumeView.h

答案 2 :(得分:6)

要展开lanbo's answer,您还可以获取路线按钮的原始图像,并创建使用UIImageRenderingMode.AlwaysTemplate渲染模式的副本。这样就可以了解当前的tintColor

在斯威夫特:

    let volumeView = MPVolumeView()

    if let routeButton = volumeView.subviews.last as? UIButton,
        let routeButtonTemplateImage  = routeButton.currentImage?.imageWithRenderingMode(.AlwaysTemplate)
    {
        volumeView.setRouteButtonImage(routeButtonTemplateImage, forState: .Normal)
    }