Thread 1: "UIViewController is missing its initial trait collection populated during initialization. This is a serious bug, likely caused by accessing properties or methods on the view controller before calling a UIViewController initializer. View controller: <TabBarViewController: 0x101014800>"
出现问题的源代码
- (instancetype)init {
ViewController *vc = [ViewController new];
//设置图片
UIImage *firstUnSelectedImage = [UIImage imageNamed:@"HomeUnSelected"];
//不设置imageWithRenderingMode,tabbar的选中色为蓝色,因为tabbar默认是蓝色的包括图片和文字,所以要解决这个问题就需要我们去掉SelectedImage的渲染效果
UIImage *firstSelectedImage = [[UIImage imageNamed:@"HomeSelected"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
//设置tabbar的各个元素
vc.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"首页" image:firstUnSelectedImage selectedImage:firstSelectedImage];
SecondViewController *vc2 = [SecondViewController new];
UIImage *secondUnSelectedImage = [UIImage imageNamed:@"MyUnSelected"];
UIImage *secondSelectedImage = [[UIImage imageNamed:@"MySelected"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
vc2.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"我的" image:secondUnSelectedImage selectedImage:secondSelectedImage];
//创建一个数组 接受各个viewController
NSArray *arr = @[vc, vc2];
NSMutableArray *array = [[NSMutableArray alloc] init];
for (UIViewController *viewController in arr) {
//让每个ViewController上面都有一个导航控制器
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:viewController];
//把导航控制器放进一个可变数组
[array addObject:nav];
}
self.viewControllers = array;
self.tabBar.backgroundColor = [UIColor HexColor:@"#F6F6F6"];
//TabBar主题色,用于修改文字颜色
self.tabBar.tintColor = [UIColor HexColor:@"#333333"];
// tabBar.delegate = self;
return self;
}
解决:
在init中的代码里加上 self = [super init]; 再运行,问题解决。