繁体   English   中英

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

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

我有一个带有音频控件的UIToolbar。 在iOS 7升级前后的某个时间,栏的颜色发生了变化,iOS开始以不同的方式为我的按钮添加阴影。 这就引入了一个错误,其中我以编程方式更改的播放和暂停按钮与工具栏的新外观不匹配:

工具栏开始看起来不错:

工具栏开始看起来不错

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

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

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

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

我希望播放和暂停按钮具有与其他按钮相同的深色外观。 我认为在构建替换按钮时,我必须做一些不同的事情。 原始图标的图像都是较浅的颜色,但iOS工具栏似乎已自动从情节提要中重新着色为较暗的颜色,如第一个屏幕截图所示。

这是我用来构建按钮的代码:

- (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];
    }
}

感谢您的帮助。

检查以下内容:

如果图像存储在图像资源中,请确保它们是模板图像

然后,您可以A)在Interface Builder中将其色调设置为您喜欢的灰色,或B)像这样在makeBarButton方法中以编程方式进行设置;

button.tintColor = [UIColor grayColor];

暂无
暂无

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

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