2013-09-29 31 views
5

我设计的iOS 7的音乐应用程序,我想直接把“AirPlay的”路由选择器按钮,在我的应用程序。我能够将按钮放置得很好,但它不会显示,因为图标是白色的,我的背景是白色的。MPVolumeView路线按钮的iOS改色7

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

这里是我使用创建按钮的代码。

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

回答

5

创建自己的形象,尝试 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 
+0

这个伎俩!原来是相当简单的解决方案,谢谢你指点我正确的解决方案! – jrwagz

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) 
    } 
+0

它的工作原理可以确定,最后一个视图是图像? – Karsten

+0

不,没有保证,但'if let'子句确保它不是'UIButton'就是安全的。 –

+0

为什么是volumeView.routeButtonImageForState(UIControlState.Normal)?。imageWithRenderingMode(UIImageRenderingMode.AlwaysTemplate);不工作? –

6

审查亚当斯回答我喜欢这项工作更清晰。所以,我的安全守卫的代码位:

的Objective-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属性的反应,如果苹果决定添加其他按钮或更改此代码的序列仍将工作。

+0

这对我来说非常合适!我眼中最好的解决方案!谢谢! – funkenstrahlen