gpt4 book ai didi

ios - 更改 NavigationController 中 UIButton 的颜色

转载 作者:行者123 更新时间:2023-11-29 12:06:26 26 4
gpt4 key购买 nike

我设置了 NavigationBar 的标题并更改了它的颜色,使用以下代码成功:

self.navigationController.navigationBar.barTintColor = [UIColor blackColor];
self.title = @"SHARE";
[self.navigationController.navigationBar setTitleTextAttributes:
@{NSForegroundColorAttributeName:[UIColor whiteColor]}];
self.edgesForExtendedLayout =UIRectEdgeNone;

如您所见,条形颜色为黑色,标题颜色为白色。现在我想在 ir 的右边添加一个按钮。按钮已成功添加,但未更改颜色。

UIButton *forwdButton = [[UIButton alloc] init];
forwdButton.titleLabel.text = @"Send";
[forwdButton.titleLabel setTintColor:[UIColor whiteColor]];
[forwdButton setShowsTouchWhenHighlighted:TRUE];
[forwdButton addTarget:self action:@selector(someMethod) forControlEvents:UIControlEventTouchDown];
UIBarButtonItem *barFrwdItem = [[UIBarButtonItem alloc] initWithCustomView:forwdButton];
self.navigationItem.hidesBackButton = TRUE;

self.navigationItem.rightBarButtonItem = barFrwdItem;

在这段代码中,我在这些行中遇到了问题:

forwdButton.titleLabel.text = @"Send";
[forwdButton.titleLabel setTintColor:[UIColor whiteColor]];

它仍然显示一个黑色按钮。它没有改变它的颜色。
我已尝试执行以下操作:

[barFrwdItem setTintColor:[UIColor whiteColor]]; //Which is the UIBarButtonItem

还有

[forwdButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal]; // which is the UIButton

最佳答案

您以错误的方式添加按钮。请像这样添加正确的按钮。

UIBarButtonItem *chkmanuaaly = [[UIBarButtonItem alloc]initWithTitle:@"Send" style:UIBarButtonItemStylePlain target:self action:@selector(someMethod)];
self.navigationItem.rightBarButtonItem=chkmanuaaly;

然后添加这一行来改变颜色

[chkmanuaaly setTitleTextAttributes:
[NSDictionary dictionaryWithObjectsAndKeys:
[UIColor whiteColor], UITextAttributeTextColor,nil]
forState:UIControlStateNormal];
[UINavigationBar appearance].tintColor = [UIColor whiteColor];

关于ios - 更改 NavigationController 中 UIButton 的颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34816215/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com