更改导航栏按钮文本颜色
我正在创建一个自定义视图来生成导航栏项目。
- (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;
}
有了这个,我得到以下输出:
我希望栏按钮具有与导航栏相同的背景颜色及其黑色文本。我尝试过使用 UINavigationController 委托以任何一种方式执行此操作,但没有成功。 我需要做出哪些改变?
I am creating a custom view to generate navigation bar items.
- (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;
}
With this I am having the following output :
I want the bar button to have same background color as navigation bar and its text in black color. I have tried doing it the either way by using UINavigationController delegate, but didn't get success.
What changes do I need to make?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这里如何更改 UIBarButtonItem 的文本标签颜色(例如黑色)(默认情况下文本标签为白色):
特别是当您选择按钮的浅色色调(背景色)时:
不要忘记,您可以还想更改文本标签阴影颜色:
(代码应放置在更改文本标签颜色本身之前,以免在文本标签颜色之上重绘阴影!)
Here how to change the text label color (e.g. to black) of a UIBarButtonItem (the text label is white colored by default):
Especially when you choose a light colored tint (background color) of the button:
And not to forget, you may also want to change the text label shadow color:
(the code should be placed before changing the text label color itself, in order not to have the shadow redrawn on top of the text label color!)
阅读 5.0 文档。 WWDC '11 召开了一次会议,讨论了一个新的、更简单的方法来做到这一点。 (课程 114 - 自定义 UIKit 控件的外观)。
Read the 5.0 documentation. There was a WWDC '11 session on this very thing about a new, and much easier way, to do this. (Session 114 - Customizing the Appearance of UIKit Controls).