出于某种原因,我无法在 View 上使用 UINavigationBar,而是使用 UIToolBar。我使用带有清晰 tintColor 的虚拟左按钮和带有黑色 tintColor 的标题按钮来正确居中标题并隐藏虚拟左栏按钮,但它不再起作用并且虚拟左按钮和标题按钮都获得主 tintColor 或者如果我禁用它们,它们变成灰色。
如何将它们理想地禁用为按钮并设置正确的色调(透明和黑色)?
- (void)addToolbar
{
CGFloat barHeight = 44.0;
UIToolbar *toolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(self.view.bounds.origin.x, self.view.bounds.size.width - barHeight, self.view.bounds.size.height, barHeight)];
UIBarButtonItem *flexibleSpaceButtonItem = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace
target:nil action:nil];
// Done button on the right
UIBarButtonItem *doneButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(done)];
// dummy button on the left to have centered title
UIBarButtonItem *dummyButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:nil action:nil];
dummyButtonItem.enabled = NO;
dummyButtonItem.tintColor = [UIColor clearColor];
// title button
UIBarButtonItem *titleButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Title text here" style:UIBarButtonItemStylePlain target:nil action:nil];
titleButtonItem.enabled = NO;
titleButtonItem.tintColor = [UIColor blackColor];
// set toolbar items
toolbar.items = @[dummyButtonItem,flexibleSpaceButtonItem, titleButtonItem, flexibleSpaceButtonItem,doneButtonItem];
[self.view addSubview:toolbar];
}
最佳答案
当您为 View 指定色调时,色调会自动传播到 View 层次结构中的所有 subview 。因为 UIWindow 继承自 UIView,所以您可以通过使用如下代码设置窗口的 tint 属性来为整个应用程序指定色调颜色:
在 application:didFinishLaunchingWithOptions:
下
window.tintColor = [UIColor purpleColor];
来源 - iOS Transitions .在使用色调颜色部分
下查看关于ios - UIBarButtonItem tintColor 停止工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22299430/