2015-10-27 46 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