2013-10-02 36 views
2

我有一個應用程序,我正在遷移到iOS 7. 但是,UIBarbuttonItem沒有標題,但工作正常。 這裏是我的代碼:UIBarbuttonItem在ios上沒有標題7

UIBarButtonItem * uibbShare = [[[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"sharewhite.png"] style:UIBarButtonItemStylePlain target:self action:@selector(sharePressed:)] autorelease]; 
// uibbShare.width = 56.0; // uncommenting this doesn't change anything 
uibbShare.title = @"Share"; 

然後我添加一些這些到工具欄與之間的一些靈活的空間項目。

... 
[items addObject:flex2]; 
[items addObject:uibbShare]; 
... 

[self.toolbar setItems:items animated:NO];

在iOS 7,他們沒有標題的所有,在iOS6的所有工作正常。你不能在ios7中創建這樣的barbuttons嗎?

更新上開發論壇同樣的問題:

UIBarButtonItem can't have both title and image?

What happened to the text under toolbar icons?

編輯:(7比6)

enter image description here

編輯2:(畫面從顯示,似乎文本消失了,並且幀/邊界是0 WTF)

enter image description here

+0

我可以在下面看到的圖片比isHidden被選中。可能是這個問題? – RFG

+0

它可能是,但它是一個內部視圖,不能(也不想)訪問它......奇怪的是,我不把它藏在任何地方,不要僅在上面的代碼行中使用工具欄在'viewDidLoad'中。如果我初始化它與標題(沒有圖像),它顯示文本...我想要兩個... – Templar

+0

你可以以編程方式訪問此屬性並修改它?類似於UIButtonLabel.hidden = NO。 – RFG

回答

0
NSDictionary *attributes = [NSDictionary dictionaryWithObjectsAndKeys:[UIFont 
    fontWithName:@"YOURFONT" size:14], NSFontAttributeName, 
    [UIColor whiteColor], NSForegroundColorAttributeName, nil]; 

[[UINavigationBar的外觀] setTitleTextAttributes:屬性]; 關鍵是使用NSFontAttributeName等等。我假設他們正在轉向使用NS品種來實現64位兼容。上面的代碼我iOS7設備上工作

+1

好吧,這改變了導航欄,而不是'UIToolbar'。 – Templar

0

嘗試修改按鈕的外觀:

[[UIBarButtonItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys: [UIColor textColor], UITextAttributeTextColor, [UIFont yourFont and yourSizeSont], UITextAttributeFont, [UIColor shadowColor], UITextAttributeTextShadowColor, [NSValue valueWithCGSize:CGSizeMake(0.0, -1.0)], UITextAttributeTextShadowOffset, nil] forState:UIControlStateNormal]; 

繼我最後的評論,我想在這樣的事情:

NSArray *views = [_UIToolbarNavigationButton subviews]; 

for (UIView *view in views) { 
    if ([view isKindOfClass:[UIButtonLabel class]]) 
    { 
     view.hidden = NO; 
    } 
} 

我不知道它是否有用。

+0

這會更改導航欄上的「UIBarButtonItems」,但在我的工具欄上,項目仍然是相同的,沒有標題:( – Templar

1

最終最終爲UIBarButtonItems創建了自定義視圖。這不是很好,但它暫時工作。我只是將舊的UIBarButtonItems傳遞給返回一個新的函數。

注意:如果有一個標題並在下面添加一個簡單的標籤,而不是實際上混淆了titleEdgeInsets和居中,我只需移動按鈕的圖像。另外,我將寬度設置爲默認值56.

-(UIBarButtonItem *)convertToButton:(UIBarButtonItem *)original 
{ 
    if (SYSTEM_VERSION_LESS_THAN(@"7.0")) 
     return original; 

    CGFloat textFieldHeight = 13.f; 
    UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom]; 
    button.frame = CGRectMake(0, 0, 56, self.toolbar.frame.size.height); 
    button.showsTouchWhenHighlighted = YES; 
    [button setImage:original.image forState:UIControlStateNormal]; 
    [button addTarget:original.target action:original.action forControlEvents:UIControlEventTouchUpInside]; 
    button.tag = original.tag; 

    UIView * wr = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, 56, self.toolbar.frame.size.height)] autorelease]; 
    [wr addSubview:button]; 
    wr.backgroundColor = [UIColor clearColor]; 

    if (original.title != nil) 
    { 
     button.imageEdgeInsets = UIEdgeInsetsMake(-7, 0, 0, 0); 
     UILabel * l = [[[UILabel alloc] initWithFrame:CGRectMake(0, self.toolbar.frame.size.height - textFieldHeight, 56, textFieldHeight)] autorelease]; 
     l.font = [UIFont systemFontOfSize:11.f]; 
     l.backgroundColor = [UIColor clearColor]; 
     l.textColor = [UIColor lightGrayColor]; 
     l.textAlignment = UITextAlignmentCenter; 
     l.text = original.title; 
     [wr addSubview:l]; 
    } 

    UIBarButtonItem * theNew = [[[UIBarButtonItem alloc] initWithCustomView:wr] autorelease]; 
    theNew.tag = original.tag; 
    theNew.width = 56; 
    return theNew; 

}