2011-07-12 146 views
0

我正在創建自定義視圖以生成導航欄項目。更改導航欄按鈕文本顏色

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil 
{ 
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; 
    if (self) { 
     // Custom initialization 
     UIView *backButtonView = [[UIView alloc] initWithFrame:CGRectMake(0,0,70,35)]; 
     UIButton *myBackButton = [[UIButton buttonWithType:UIButtonTypeRoundedRect] retain]; 
     [myBackButton setFrame:CGRectMake(0,0,70,35)]; 
     myBackButton.titleLabel.text = @"Back"; 
     myBackButton.backgroundColor = [UIColor yellowColor]; 
     myBackButton.titleLabel.textAlignment = UITextAlignmentCenter; 
     myBackButton.titleLabel.textColor = [UIColor blackColor]; 
     [myBackButton setEnabled:YES]; 
     [myBackButton addTarget:self.navigationController action:@selector(moveBack:) forControlEvents:UIControlEventTouchUpInside]; 
     [backButtonView addSubview:myBackButton]; 
     [myBackButton release]; 
     UIBarButtonItem* backButton = [[UIBarButtonItem alloc] initWithCustomView:backButtonView]; 
     self.navigationItem.leftBarButtonItem = backButton; 
     [backButtonView release]; 
     [backButton release]; 


     CGRect frame = CGRectMake(300, 0, 400, 35); 
     UILabel *label = [[[UILabel alloc] initWithFrame:frame] autorelease]; 
     label.backgroundColor = [UIColor yellowColor]; 
     label.font = [UIFont boldSystemFontOfSize:20.0]; 
     label.shadowColor = [UIColor colorWithWhite:0.0 alpha:0.5]; 
     label.textAlignment = UITextAlignmentCenter; 
     label.textColor = [UIColor blackColor]; 
     label.text = @"Order booking"; 
     self.navigationItem.titleView = label; 
    } 
    return self; 
} 

有了這個,我有以下的輸出:

enter image description here

我想欄按鈕有相同的背景色導航欄及其在黑色文本。我曾嘗試通過使用UINavigationController代表,但無法獲得成功。 我需要做些什麼改變?請協助。

Nitish

回答

1

閱讀5.0文檔。關於這個事情,有一個關於新的,更容易的方法的WWDC '11會議來做到這一點。 (會話114 - 自定義UIKit控件的外觀)。

+0

我在哪裏可以下載或查看5.0 documention.may我安裝這個在Xcode 3,請幫助me.where我可以下載WWDC.thanku所有新的示例代碼。我等待烏拉圭回合早日答覆 –

+0

你可以在這裏找到所有的會話(https://developer.apple.com/videos/wwdc/2011/)。會話114被稱爲「自定義UIKit控件的外觀」。您需要使用Apple ID登錄才能看到它。 – ettore

+0

要編碼iOS 5 SDK,您需要Xcode 4.2。目前,會話中有一些示例代碼,但迄今爲止,僅供WWDC與會者使用。我猜新版iOS 5 API的文檔尚未完成,尚未發佈。儘管如此,會話確實會引導您閱讀大量代碼。 –

3

這裏如何更改的UIBarButtonItem的文本標籤的顏色(如黑色)(文本標籤白色默認):

[self.myBarButton setTitleTextAttributes:[NSDictionary dictionaryWithObject:[UIColor blackColor] forKey:UITextAttributeTextColor] forState:UIControlStateNormal]; 

尤其是當你選擇淺色色調(背景色)

self.myBarButton.tintColor = [UIColor yellowColor]; 

而且不要忘記,你也可能要改變的文本標籤陰影顏色: (代碼應放在改變文本標籤的顏色本身之前,爲了不有陰影的按鈕重新繪製在文本標籤的頂部色彩!)

[self.fullVersionViewButton setTitleTextAttributes:[NSDictionary dictionaryWithObject:[UIColor grayColor] forKey:UITextAttributeTextShadowColor] forState:UIControlStateNormal];