更改导航栏按钮文本颜色

发布于 2024-11-19 18:12:40 字数 1948 浏览 3 评论 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;
}  

有了这个,我得到以下输出:

在此处输入图像描述

我希望栏按钮具有与导航栏相同的背景颜色及其黑色文本。我尝试过使用 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 :

enter image description here

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

新雨望断虹 2024-11-26 18:12:40

这里如何更改 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];

Here how to change the text label color (e.g. to black) of a UIBarButtonItem (the text label is white colored by default):

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

Especially when you choose a light colored tint (background color) of the button:

self.myBarButton.tintColor = [UIColor yellowColor];

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!)

[self.fullVersionViewButton setTitleTextAttributes:[NSDictionary dictionaryWithObject:[UIColor grayColor] forKey:UITextAttributeTextShadowColor] forState:UIControlStateNormal];
你没皮卡萌 2024-11-26 18:12:40

阅读 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).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文