我正在尝试使用插件在我的应用中的UINavigationBar中有一个Gradient。我在https://github.com/chroman/CRGradientNavigationBar
找到了它我的应用是一个标签栏控制器应用,其中每个标签都是一个NavigationController。我的代码设置为:
UINavigationController *navigationController = [[UINavigationController alloc] initWithNavigationBarClass:[CRGradientNavigationBar class] toolbarClass:nil];
UIColor *firstColor = [UIColor colorWithRed:255.0f/255.0f green:42.0f/255.0f blue:104.0f/255.0f alpha:1.0f];
UIColor *secondColor = [UIColor colorWithRed:255.0f/255.0f green:90.0f/255.0f blue:58.0f/255.0f alpha:1.0f];
NSArray *colors = [NSArray arrayWithObjects:firstColor, secondColor, nil];
// or NSArray *colors = [NSArray arrayWithObjects:(id)UIColorFromRGB(0xf16149).CGColor, (id)UIColorFromRGB(0xf14959).CGColor, nil];
[[CRGradientNavigationBar appearance] setBarTintGradientColors:colors];
[[navigationController navigationBar] setTranslucent:NO]; // Remember, the default value is YES.
[navigationController setViewControllers:@[tabBarController]];
[self.window setRootViewController:navigationController];
然而,我得到双导航栏,标题和uibarbuttonitems被添加到第二个,而不是渐变。有什么建议吗?
答案 0 :(得分:0)
UITabBarController不应该是rootViewController和UINavigationController。尝试将tabBarController设置为窗口的rootViewController,如下所示:
[self.window setRootViewController:tabBarController];
然后,您需要使用导航控制器填充选项卡栏控制器上的viewControllers数组,这会导致每个选项卡都有自己的导航栏和视图控制器堆栈:
UINavigationController *navigationController = [[UINavigationController alloc] initWithNavigationBarClass:[CRGradientNavigationBar class] toolbarClass:nil];
[tabBarController setViewControllers:@[navigationController]];
这将导致标签栏控制器底部显示一个选项卡,该选项卡顶部有一个导航栏。如果您想要更多选项卡,只需将更多视图控制器实例添加到您在tabBarController上设置的viewControllers数组中:
UINavigationController *navigationController1 = [[UINavigationController alloc] initWithNavigationBarClass:[CRGradientNavigationBar class] toolbarClass:nil];
UINavigationController *navigationController2 = [[UINavigationController alloc] initWithNavigationBarClass:[CRGradientNavigationBar class] toolbarClass:nil];
UINavigationController *navigationController3 = [[UINavigationController alloc] initWithNavigationBarClass:[CRGradientNavigationBar class] toolbarClass:nil];
[tabBarController setViewControllers:@[navigationController1,navigationController2,navigationController3]];
这些导航控制器中的每一个都将从堆栈中的至少1个UIViewController开始,如下所示:
UINavigationController *navigationController = [[UINavigationController alloc] initWithNavigationBarClass:[CRGradientNavigationBar class] toolbarClass:nil];
UIViewController* viewControllerForNavigationController = [UIViewController new];
[navigationController setViewControllers:@[viewControllerForNavigationController]];