在自定义导航栏中设置栏按钮项颜色
我使用XIB创建了自定义导航栏和右侧导航按钮。这工作正常。但我需要自定义右侧导航按钮的色彩颜色。此时此色调与导航栏的色调颜色相同。我需要为这个右按钮使用不同的颜色。有什么方法可以改变这种颜色吗?在自定义导航栏中设置栏按钮项颜色
谢谢。
不知道这个按钮是否继承了navigationBar的tint颜色。你可以做的是设置一个customView为navigationItem:
这是你如何使用SDK的一个按钮设置:
UIBarButtonItem *shareButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction target:self action:@selector(shareButtonHandler:)];
[self.navigationItem setRightBarButtonItem:shareButton];
[shareButton release];
相反,你可以做这样的:
UIBarButtonItem *btn = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"yourImage.png" style:UIBarButtonItemStyleBordered target:self action:@selector(customButtonHandler:)]];
使用您在Photoshop等制作的图像
还有一个initWithCustomView:UIView
或initWithTitle:NSString
您可以使用。
对不起没有 “单行溶液” :)
在iOS 5中有 '的出现' 功能:
[[UIBarButtonItem appearance] setTintColor:YOUR_COLOR];
是的,这是一个非常古老的线程。在旧的操作系统中并不那么容易。 – Dilshan 2012-05-15 17:59:02
夫特3.x的
UIBarButtonItem.appearance().setTitleTextAttributes([NSForegroundColorAttributeName : Color.primaryActionColor], for: .normal)
这里的问题是,每个UIBarButtonItem都将具有该颜色。为了避免这种情况,你可以这样做:UIBarButtonItem.appearance(whenContainedInInstancesOf:[UIToolbar.self])。setTitleTextAttributes(...) – Reyske 2018-01-25 10:51:37
是这是最接近的答案。没有其他办法可以做到这一点。谢谢你RickiG。 – Dilshan 2010-12-28 10:05:44