突出显示 UIBarButtonItem,如 iOS7.1 日历 - 已经尝试过 setBackgroundImage

Highlight UIBarButtonItem like iOS7.1 calendar - already tried setBackgroundImage

我试图让 UIBarButtonItem 的行为类似于 iOS 7 日历应用程序中的 "view mode" 按钮。点击时,它会突出显示并保持这种状态,直到再次点击。

Example image(还不能嵌入图片,抱歉)

我已经按照建议将背景图像 属性 设置为带有 forState:(UIControlStateHighlighted | UIControlStateSelected) 的图像 here,没有任何反应。

我认为您应该使用自定义视图。使用 40x40 按钮查看此示例

    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(0,0,40,40);

    [button setBackgroundImage:[UIImage imageNamed:@"button.png"]  forState:UIControlStateNormal];
    [button setBackgroundImage:[UIImage imageNamed:@"buttonSelected.png"]  forState:UIControlStateSelected];
    [button addTarget:self action:@selector(didTouchButton:) forControlEvents:UIControlEventTouchUpInside];

    UIBarButtonItem *buttonItem = [[UIBarButtonItem alloc] initWithCustomView:button];
    [self.navigationItem setRightBarButtonItems:[NSArray arrayWithObjects:buttonItem, nil]];

并在触摸集中选择

- (void) didTouchButton:(id)sender
{
    UIButton *button = (UIButton*)sender;
    button.selected = YES;

    //Do stuff
}