UItabbarController自定义 常用架构 UITabBar UITabBaritem

2017-09-12 19:34:17来源:CSDN作者:zhw521411人点击

分享
  • (BOOL)application:(UIApplication )application didFinishLaunchingWithOptions:(NSDictionary )launchOptions {

    self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
    self.window.backgroundColor = [UIColor whiteColor];

    //1.
    ViewController *v = [[ViewController alloc] init];
    UINavigationController *nV = [[UINavigationController alloc] initWithRootViewController:v];
    //系统
    // UITabBarItem *item1 = [[UITabBarItem alloc] initWithTabBarSystemItem:UITabBarSystemItemFavorites tag:1000];
    // UITabBarItem *item1 = [[UITabBarItem alloc] initWithTitle:@”系统” image:[[UIImage imageNamed:@”btn_expression_h”] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] tag:1000];

    UITabBarItem *item0 = [[UITabBarItem alloc] initWithTitle:@”系统” image:[[UIImage imageNamed:@”btn_expression_n”] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] selectedImage:[[UIImage imageNamed:@”btn_expression_h”] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];

    //2.
    NextViewController *next = [[NextViewController alloc] init];
    UINavigationController *nN = [[UINavigationController alloc] initWithRootViewController:next];
    UITabBarItem *item1 = [[UITabBarItem alloc] initWithTitle:@”自定义” image:[[UIImage imageNamed:@”btn_light_n”] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] selectedImage:[[UIImage imageNamed:@”btn_light_h”] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];

    //3.
    OtherViewController *other = [[OtherViewController alloc] init];
    UINavigationController *nO = [[UINavigationController alloc] initWithRootViewController:other];

    UITabBarItem *item2 = [[UITabBarItem alloc] initWithTitle:@”3” image:[[UIImage imageNamed:@”btn_sound_n”] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] selectedImage:[[UIImage imageNamed:@”btn_sound_h”] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];

    UITabBarController *tabBarC = [[UITabBarController alloc] init];
    tabBarC.viewControllers = @[nV, nN, nO];

    v.tabBarItem = item0;
    next.tabBarItem = item1;
    other.tabBarItem = item2;

    //设置tabbar背景颜色
    [[UITabBar appearance] setBarTintColor:[UIColor cyanColor]];
    //[UITabBar appearance].translucent = NO;
    //tabBarC.tabBar.translucent = NO;
    //或者
    // UIView *vi = [[UIView alloc] init];
    // vi.backgroundColor = [UIColor orangeColor];
    // vi.frame = tabBarC.tabBar.bounds;
    // [[UITabBar appearance] insertSubview:vi atIndex:0];
    //4.设置tabBar的背景图片

    // Change the tab bar background
    //[[UITabBar appearance] setBackgroundImage:[UIImage imageNamed:@”home_bluetooth_btn_”]];
    // UIImage* tabBarBackground = [UIImage imageNamed:@”home_bluetooth_btn_”];
    // [[UITabBar appearance] setBackgroundImage:[tabBarBackground resizableImageWithCapInsets:UIEdgeInsetsZero]];
    //[[UITabBar appearance] setSelectionIndicatorImage:[UIImage imageNamed:@”btn_light_h”]];
    //[[UITabBar appearance] setUnselectedItemTintColor:[UIColor orangeColor]];

    // 改字体大小
    NSDictionary * dic = @{ NSFontAttributeName:[UIFont systemFontOfSize:18]};
    [item0 setTitleTextAttributes:dic forState:UIControlStateNormal];
    [item1 setTitleTextAttributes:dic forState:UIControlStateNormal];
    [item2 setTitleTextAttributes:dic forState:UIControlStateNormal];
    //[item4 setTitleTextAttributes:dic forState:UIControlStateNormal];
    //改变字体颜色
    [item0 setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor colorWithRed:110/255.0 green:50/255.0 blue:150/245.0 alpha:1],NSForegroundColorAttributeName, nil] forState:UIControlStateNormal];
    [item1 setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor colorWithRed:90/255.0 green:180/255.0 blue:150/155.0 alpha:1],NSForegroundColorAttributeName, nil] forState:UIControlStateNormal];
    [item2 setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor colorWithRed:120/255.0 green:230/255.0 blue:49/255.0 alpha:1],NSForegroundColorAttributeName, nil] forState:UIControlStateNormal];
    // [item3 setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor colorWithRed:150/255.0 green:150/255.0 blue:150/255.0 alpha:1],NSForegroundColorAttributeName, nil] forState:UIControlStateNormal];

//

warning 设置UItabBaritem

/*选中item背景颜色*/CGSize indicatorImgeSize = CGSizeMake(tabBarC.tabBar.bounds.size.width / tabBarC.tabBar.items.count, tabBarC.tabBar.bounds.size.height);//tabBarC.tabBar.selectionIndicatorImage = [self drawTabBarItemBackgroundImageWithSize:indicatorImgeSize ];self.window.rootViewController = tabBarC;[self.window makeKeyAndVisible];// Override point for customization after application launch.return YES;

}

  • (UIImage *)drawTabBarItemBackgroundImageWithSize:(CGSize)size {
    //准备绘画环境
    UIGraphicsBeginImageContext(size);
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    CGContextSetRGBFillColor(ctx, 124.0/255.0, 230/255.0, 49.0/255.0, 1);
    CGContextFillRect(ctx, CGRectMake(0, 0, size.width, size.height));
    //获取该绘图中的图片
    UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
    //结束绘画
    UIGraphicsEndImageContext();
    return img;
    }
    附上code4 仿 咸鱼,中间按钮凸起
    • 源代码地址
    • https://github.com/zhangchunyu2016/CYTabbar
      */

最新文章

123

最新摄影

微信扫一扫

第七城市微信公众平台