iOS-bug记录

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]; 再运行,问题解决。

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值