2011-07-12 94 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];