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

时间:2015-01-28 22:21:46

标签: ios ios8 calendar uibarbuttonitem ios7.1

我正在尝试使UIBarButtonItem的行为类似于iOS 7日历应用中的“查看模式”按钮。点击后,它会突出显示并保持这种状态,直到再次点击它。

Example image(无法嵌入图片,抱歉)

我已经尝试将BackgroundImage属性设置为具有forState的图像:(UIControlStateHighlighted | UIControlStateSelected),如建议here,没有任何反应。

1 个答案:

答案 0 :(得分:0)

我认为你应该使用自定义视图。使用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
}