//
//  XYTabBarController.m
//  Open
//
//  Created by qxy on 2021/11/9.
//  Copyright © 2021 QXY. All rights reserved.
//

#import "XYTabBarController.h"

@interface XYTabBarController ()

@end

@implementation XYTabBarController
- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setupTabBar];
}

- (NSArray *)configs{
    return @[@{@"vc"       : @"HomeViewController",
               @"title"         : @"首页",
               @"image"         : @"首页",
               @"selectedImage" : @"首页_s"
    },
             @{@"vc"            : @"HeadLineViewController",
               @"title"         : @"头条",
               @"image"         : @"资讯",
               @"selectedImage" : @"资讯_s"
             },
             @{@"vc"            : @"FindViewController",
               @"title"         : @"健康",
               @"image"         : @"life_unselect",
               @"selectedImage" : @"life_selected"
             },
             @{@"vc"            : @"Log2ViewController",
               @"title"         : @"我的",
               @"image"         : @"我的",
               @"selectedImage" : @"我的_s"
             }
    ];
}

- (void)setupTabBar{
    NSMutableArray *vcArr = [[NSMutableArray alloc] init];
    for (NSDictionary *item in self.configs) {
        Class clazz = NSClassFromString(item[@"vc"]);
        UIViewController *vc = [[clazz alloc] init];
        vc.tabBarItem.title = item[@"title"];
        
        // 设置图标，确保不会拉伸
        UIImage *normalImage = [UIImage imageNamed:item[@"image"]];
        UIImage *selectedImage = [UIImage imageNamed:item[@"selectedImage"]];
        
        // 设置图标大小为标准TabBar图标大小
        CGSize iconSize = CGSizeMake(25, 25);
        UIGraphicsBeginImageContextWithOptions(iconSize, NO, 0.0);
        [normalImage drawInRect:CGRectMake(0, 0, iconSize.width, iconSize.height)];
        UIImage *resizedNormalImage = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
        
        UIGraphicsBeginImageContextWithOptions(iconSize, NO, 0.0);
        [selectedImage drawInRect:CGRectMake(0, 0, iconSize.width, iconSize.height)];
        UIImage *resizedSelectedImage = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
        
        vc.tabBarItem.image = [resizedNormalImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
        vc.tabBarItem.selectedImage = [resizedSelectedImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
        
        UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:vc];
        [vcArr addObject:nav];
    }
    self.viewControllers = [NSArray arrayWithArray:vcArr];
    self.tabBar.translucent = NO;
    self.tabBar.tintColor = K_MainColor;
    
    // 设置TabBar外观
    if (@available(iOS 15.0, *)) {
        UITabBarAppearance *appearance = [[UITabBarAppearance alloc] init];
        appearance.configureWithOpaqueBackground;
        appearance.backgroundColor = [UIColor whiteColor];
        
        // 设置图标位置和大小
        appearance.stackedLayoutAppearance.normal.iconColor = [UIColor grayColor];
        appearance.stackedLayoutAppearance.selected.iconColor = K_MainColor;
        
        self.tabBar.standardAppearance = appearance;
        self.tabBar.scrollEdgeAppearance = appearance;
    }
    
    //NSLog(@"z主界面开始更新了");
    UIImageView *imv = [[UIImageView alloc]init];
    imv.frame = CGRectMake(0, 0, self.tabBar.bounds.size.width, TAB_BAR_HEIGHT);
    imv.backgroundColor = [UIColor whiteColor];
    [[UITabBar appearance] insertSubview:imv atIndex:0];
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
