1. 程式人生 > >UItabbarController自定義 常用架構 UITabBar UITabBaritem

UItabbarController自定義 常用架構 UITabBar UITabBaritem

  • (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 仿 鹹魚,中間按鈕凸起