在没有 xib 文件的情况下更改我的选项卡栏颜色



我在更改选项卡栏颜色时遇到问题,因为我没有使用 xib 文件,而且不知何故我的代码结构根本没有帮助我,告诉我是否需要从头开始重写整个事情,但是如果我不必 ^_^

这是我的单视图应用程序的appDelegate文件,在这里我创建选项卡栏,其中包含与每个选项卡关联的导航栏,然后显示它们

self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
[self.window makeKeyAndVisible];
self.firstTab = [[FirstTab alloc] initWithNibName:nil bundle:NULL];
self.firstNavigationController = [[UINavigationController alloc] initWithRootViewController:self.firstTab];
self.thirdTab = [[ThirdTab alloc] initWithNibName:nil bundle:NULL];
self.thirdNavigationController = [[UINavigationController alloc] initWithRootViewController:self.thirdTab];
self.tab2 = [[newsecViewController alloc] initWithNibName:@"newsecViewController" bundle:NULL];
self.tab2NavigationController = [[UINavigationController alloc] initWithRootViewController:self.tab2];
self.tab2.view.backgroundColor = [UIColor purpleColor];

//NSArray *tabs = [[NSArray alloc] initWithObjects:self.firstTab, self.secondTab, self.thirdTab, nil];
NSArray *tabBars = [[NSArray alloc] initWithObjects:self.firstNavigationController, self.tab2NavigationController, self.thirdNavigationController, nil];
self.tabBarController = [[UITabBarController alloc] init];
[self.tabBarController setViewControllers:tabBars];
[self.window addSubview:self.tabBarController.view];

这是我的一个选项卡的代码示例

self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if(self != nil) {
    self.title = @"Second";
    self.tabBarItem.image = [UIImage imageNamed:@"triangle.png"];
}
return self;

谢谢大家的帮助...

使用标签栏的 tintColor 属性。它在 iOS 5.0 及更高版本中可用。

[tabBarController.tabBar setTintColor:[UIColor purpleColor]];

从 iOS 7 开始,在设置UINavigationBarUITabBar的颜色时,请使用 barTintColor 属性。

[self.tabBarController.tabBar setBarTintColor:[UIColor purpleColor]];

最新更新