2015-10-27 35 views
1

the screen shot of my project如何取消的UIBarButtonItem的setTitle闪亮效果

像形象示人,我用的UIBarButtonItem作为rightBarButtonItem,我想要做的就是改变rightBarButton的标题时,我选择照片或取消选择的图片,所以我用这样的代码这样的:

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

,但是当它的文本改变了barbutton将显示护腿,我尝试使用的UIButton代替的UIBarButtonItem和趋肤效应确实消失了,但的UIButton将接近正确的边界远,你能帮帮我吗?

thr screen shot of my project with UIButton

+0

尝试通过访问其标题属性来设置按钮标题: –

+0

但我更改barbuttonitem的标题,将会有闪亮的动画,并且我不需要该效果。 – gavinHe

回答

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 
+0

不应该使用UIButton * rightButton = [UIButton buttonWithType:UIButtonTypeCustom]? – gavinHe

+0

和我用setTitlePositionAdjustment方法,但我看不到任何效果,有什么我犯错吗? – gavinHe

+0

@gavinHe是的,你也可以使用buttonWithType:UIButtonTypeCustom。你可以告诉我你是如何使用“setTitlePositionAdjustment”? – RoHaN

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,您的想法确实给了我很大启发。

+0

很高兴我能帮忙,谢谢。 – RoHaN