简体   繁体   English

为什么 tintColor 在 iOS7 上的导航栏或工具栏上不起作用

[英]Why tintColor doesn't work on navigation bar or toolbar on iOS7

On iOS7 of iPad, first, I setup an modal view controller, the size is 320 * 460, then, in this modal view controller, I presented another navigation view controller, after this, the tint color of navigation bar and tool bar of the presented navigation controller turns gray.在 iPad 的 iOS7 上,首先我设置了一个模态视图控制器,大小为 320 * 460,然后,在这个模态视图控制器中,我展示了另一个导航视图控制器,然后是导航栏和工具栏的色调颜色呈现的导航控制器变为灰色。 I have tried to set tint color of navigation bar and tool bar, but it just doesn't work.我试图设置导航栏和工具栏的色调颜色,但它不起作用。

Then I tried to present the navigation controller directly, then all tint color works both on navigation bar and tool bar.然后我尝试直接呈现导航控制器,然后所有色调颜色都适用于导航栏和工具栏。

I have tried with the barTintColor property of navigation bar and tool bar, it works.我已经尝试使用导航栏和工具栏的barTintColor属性,它有效。

I don't know what happens.我不知道会发生什么。

Update更新

first, I define a view controller: modalViewController The present the modal view controller like this:首先,我定义了一个视图控制器:modalViewController 像这样呈现模态视图控制器:

if (DeviceIsPad()) // DeviceIsPad is a method defined somewhere to tell that the device is an iPad.
    modaViewController.modalPresentationStyle = UIModalPresentationFormSheet;

//here self is a normal view controller
[self presentViewController:modalViewController animated:YES completion:NULL];

Second, define a navigation view controller: navigationController Present the navigation controller like this:其次,定义一个导航视图控制器: navigationController 像这样呈现导航控制器:

if (DeviceIsPad())
    navigationController.modalPresentationStyle = UIModalPresentationCurrentContext;

// here self means the modalViewController mentioned above
[self presentViewController:navigationController animated:YES completion:nil];

I setup the navigation bar and toolbar bar items in 'viewDidLoad' method of navigationController.我在 navigationController 的“viewDidLoad”方法中设置了导航栏和工具栏栏项目。

By default, when the navigation view controller comes out, all toolbar button items(The items are built with just basic title like Cancel , OK ) turns to be gray.默认情况下,当导航视图控制器出现时,所有工具栏按钮项目(这些项目仅使用基本标题构建,如CancelOK )变为灰色。

At the same time, I have tried to set tintColor of tool bar and navigation bar .同时,我尝试设置tool barnavigation bar tintColor Both instance method and appearance method(like [[UIToolBar appearance] setTintColor:[UIColor blueColor]] ) are used.使用实例方法和外观方法(如[[UIToolBar appearance] setTintColor:[UIColor blueColor]] )。 But it still doesn't work.但它仍然不起作用。

And then I tried to present the navigationViewController mentioned above with UIModalPresentationFormSheet style directly from a normal view controller, then all tintColor for navigation bar and tool bar turns to be the blue color(the system blue color).然后我尝试直接从普通视图控制器中使用UIModalPresentationFormSheet样式呈现上面提到的navigationViewController ,然后导航栏和工具栏的所有 tintColor 变成蓝色(系统蓝色)。

This happens because Apple assumes that we won't present more than 1 view controller and dims the background and ALL bar button items (not sure why) as default behavior to put focus on the frontmost view.发生这种情况是因为 Apple 假设我们不会呈现超过 1 个视图控制器并将背景和所有栏按钮项(不知道为什么)变暗作为将焦点放在最前面的视图的默认行为。

To fix this, you just need to force the tintAdjustmentMode to Normal on the app's window in DidFinishLaunchingWithOptions.要解决此问题,您只需在应用程序窗口的 DidFinishLaunchingWithOptions 中将 tintAdjustmentMode 强制设为 Normal。

self.window.tintAdjustmentMode = UIViewTintAdjustmentModeNormal;

Try setting尝试设置

 [[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:@"NavBarBGTile.png"] forBarPosition:UIBarPositionTopAttached barMetrics:UIBarMetricsDefault];

Where NavBarBGTile.png is a 1X1px tile image that in the color you want as navigation bar color其中 NavBarBGTile.png 是一个 1X1px 平铺图像,其颜色为您想要的导航栏颜色

Also

  [[UINavigationBar appearance] setTintColor:[UIColor whiteColor]];

Replace white with whatever you want用你想要的任何东西替换白色

These lines should place at the begining of application launch这些行应该放在应用程序启动的开始处

Try This Code:试试这个代码:

UIView *masterView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 300, 260)];
UIToolbar *pickerToolbar = [self createPickerToolbarWithTitle:@"Surgeons"];
pickerToolbar.barTintColor= [UIColor blueColor];
pickerToolbar.translucent=YES;
[masterView addSubview:pickerToolbar];
[masterView addSubview:pickerViewObj];
UIViewController *viewController = [[UIViewController alloc] initWithNibName:nil bundle:nil];
viewController.view = masterView;
viewController.contentSizeForViewInPopover = viewController.view.frame.size;
self.popoverController =[[UIPopoverController alloc] initWithContentViewController:viewController];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM