如何取消UIBarbuttonItem setTitle闪亮效果

时间:2015-10-27 06:31:20

标签: ios title uibarbuttonitem effect

the screen shot of my project

与图像显示一样,我使用UIBarbuttonItem作为rightBarButtonItem,我想要做的只是在选择图片或取消选择图片时更改rightBarButton的标题,所以我使用了这样的代码:

[self.rightBarButton setTitle:[NSString stringWithFormat:@"完成(%@/%@)",@(_selectedAssets.count),@(self.maxPicturesNum)]];

但是当文本改变时,按钮会显示shin,我尝试使用UIButton而不是UIBarButtonItem,而且shin效果确实消失了,但是UIButton会接近右边界,你能帮帮我吗?

thr screen shot of my project with UIButton

2 个答案:

答案 0 :(得分:0)

您可以创建UIButton并将其分配给rightBarButton

UIButton *rightButton = [[UIButton alloc] initWithFrame:someFrame];
[rightButton setBackgroundImage:someImage forState:UIControlStateNormal];
[rightButton addTarget:self action:@selector(back:) forControlEvents:UIControlEventTouchUpInside];
rightButton.titleLabel.text = [NSString stringWithFormat:@"完成(%@/%@)",@(_selectedAssets.count),@(self.maxPicturesNum)];
rightButton.titleLabel.font = someFont;
UIBarButtonItem *rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:rightButton];
self.rightBarButton = rightBarButtonItem;

在更新标签时使用rightButton.titleLabel.text

对于调整标签,请使用:

- (void)setTitlePositionAdjustment:(UIOffset)adjustment
                 forBarMetrics:(UIBarMetrics)barMetrics

答案 1 :(得分:0)

最后,我喜欢这个:

- (void)loadRightButtonItem{
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(30, 0, 120, 40);
button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
[button setTitle:@"完成(0/9)" forState:UIControlStateNormal];
button.tintColor = [UIColor whiteColor];
[button setTitleShadowColor:[UIColor whiteColor] forState:UIControlStateHighlighted];
//    [button setTitleColor:[self.buttonFolder titleColorForState:UIControlStateHighlighted] forState:UIControlStateHighlighted];
[button addTarget:self action:@selector(onFinishClicked:) forControlEvents:UIControlEventTouchUpInside];

UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 135, 40)];
UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithCustomView:view];

[view addSubview:button];
self.navigationItem.rightBarButtonItem = item;
_rightBarButton = button;
}

取消胫骨效果。

感谢@ RoHaN,你的想法给我带来了很大的启发。