简体   繁体   English

否以编程方式构建的播放/暂停按钮的barButton项目色调不正确

[英]Wrong barButton item tint for play/pause buttons built programatically

I have a UIToolbar with audio controls. 我有一个带有音频控件的UIToolbar。 Sometime around iOS 7 upgrade the color of the bar changed and iOS started shading my buttons differently. 在iOS 7升级前后的某个时间,栏的颜色发生了变化,iOS开始以不同的方式为我的按钮添加阴影。 That introduced a bug where the play and pause buttons which I change programatically don't match the new look of the toolbar: 这就引入了一个错误,其中我以编程方式更改的播放和暂停按钮与工具栏的新外观不匹配:

Toolbar starts out looking fine: 工具栏开始看起来不错:

工具栏开始看起来不错

Now I pressed play so code inserted pause button but wrong color: 现在我按下播放,所以代码插入了暂停按钮,但是颜色错误:

现在我按了播放,所以代码插入了暂停按钮,但是颜色错误

Now I pressed pause and code inserted play button, again with wrong color: 现在,我按下了暂停键,并插入了插入播放按钮的代码,再次使用了错误的颜色:

现在,我按下暂停键,并插入了插入播放按钮的代码,再次使用了错误的颜色

I want the play and pause buttons to have that same dark look as the other buttons. 我希望播放和暂停按钮具有与其他按钮相同的深色外观。 I assume I have to do something differently when building the replacement buttons. 我认为在构建替换按钮时,我必须做一些不同的事情。 The raw icon images are all that lighter color, but iOS toolbar seems to be automatically recoloring to darker color from the storyboard, as seen in first screenshot. 原始图标的图像都是较浅的颜色,但iOS工具栏似乎已自动从情节提要中重新着色为较暗的颜色,如第一个屏幕截图所示。

Here's the code I use to build the buttons: 这是我用来构建按钮的代码:

- (UIBarButtonItem *) makeBarButton: (NSString *) imageName action:(SEL)targetAction
{
    UIImage* image = [UIImage imageNamed:imageName];
    CGRect frame = CGRectMake(0 - 20, 0 - 20, image.size.width + 20, image.size.height + 20);
    UIButton *button = [[UIButton alloc] initWithFrame:frame];
    [button addTarget:self action:targetAction forControlEvents:UIControlEventTouchUpInside];
    [button setImage:image forState:UIControlStateNormal];
    [button setShowsTouchWhenHighlighted:YES];
    return [[UIBarButtonItem alloc] initWithCustomView:button];
}

- (void) setAsPlaying:(BOOL)isPlaying
{
    self.rootViewController.playing = isPlaying;

    // we need to change which of play/pause buttons are showing, if the one to
    // reverse current action isn't showing
    if ((isPlaying && !self.pauseButton) || (!isPlaying && !self.playButton))
    {
        UIBarButtonItem *buttonToRemove = nil;
        UIBarButtonItem *buttonToAdd = nil;
        if (isPlaying)
        {
            buttonToRemove = self.playButton;
            self.playButton = nil;
            self.pauseButton = [self makeBarButton:@"icon_pause.png" action:@selector(pauseAudio:)];
            buttonToAdd = self.pauseButton;
        }
        else
        {
            buttonToRemove = self.pauseButton;
            self.pauseButton = nil;
            self.playButton = [self makeBarButton:@"icon_play.png" action:@selector(playAudio:)];
            buttonToAdd = self.playButton;
        }

        // Get the reference to the current toolbar buttons
        NSMutableArray *toolbarButtons = [[self.toolbar items] mutableCopy];

        // Remove a button from the toolbar and add the other one
        if (buttonToRemove)
            [toolbarButtons removeObject:buttonToRemove];
        if (![toolbarButtons containsObject:buttonToAdd])
            [toolbarButtons insertObject:buttonToAdd atIndex:4];

        [self.toolbar setItems:toolbarButtons];
    }
}

Appreciate your help with this. 感谢您的帮助。

Check the following: 检查以下内容:

If the images are stored inside your image assets, make sure they are template images . 如果图像存储在图像资源中,请确保它们是模板图像

Then you can either A) set their tint colour in Interface Builder to the grey colour you prefer or B) do it programmatically in your makeBarButton method as such; 然后,您可以A)在Interface Builder中将其色调设置为您喜欢的灰色,或B)像这样在makeBarButton方法中以编程方式进行设置;

button.tintColor = [UIColor grayColor];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM