UITabBarController的一些基礎設置


利用代碼添加UITabBarController

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    
    ViewController *vc1 = [[ViewController alloc] init];
    
    UserGuideViewController *vc2 = [[UserGuideViewController alloc] init];
    
    UITabBarController *tabBarController = [[UITabBarController alloc] init];
    tabBarController.viewControllers = [[NSArray alloc] initWithObjects:vc1, vc2, nil];
    
    for(int i=0; i<tabBarController.tabBar.items.count; i++) {
        UITabBarItem *item = [tabBarController.tabBar.items objectAtIndex:i];
        
        item.title = [NSString stringWithFormat:@"子欄目%d",i];
        
        NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:[UIColor grayColor],UITextAttributeTextColor, nil];
        [item setTitleTextAttributes:dict forState:UIControlStateNormal];
        
        NSDictionary *dict2 = [NSDictionary dictionaryWithObjectsAndKeys:[UIColor redColor],
                               UITextAttributeTextColor,nil];
        [item setTitleTextAttributes:dict2 forState:UIControlStateSelected];
    }
    
    self.window.rootViewController = tabBarController;
    [self.window makeKeyAndVisible];
    
    return YES;
}

 

改變TabBar的背景色

方法一:

UIView *bgView = [[UIView alloc] initWithFrame:self.tabBar.bounds];
bgView.backgroundColor = [UIColor redColor];
[self.tabBar insertSubview:bgView atIndex:0];

方法二:

CGRect rect = CGRectMake(0.0f, 0.0f, 1.0f, 1.0f);
UIGraphicsBeginImageContext(rect.size);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(context, [[UIColor redColor] CGColor]);
CGContextFillRect(context, rect);
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

self.tabBar.backgroundImage = image;

 

改變TabBarItem的(選擇/未選擇)背景圖片

方法一,改變全局:

UIImage *bgImage = [UIImage imageNamed:@"Image1"];
[[UITabBar appearance] setBackgroundImage:[bgImage resizableImageWithCapInsets:UIEdgeInsetsZero]];
[[UITabBar appearance] setSelectionIndicatorImage:[UIImage imageNamed:@"Image2"]];

方法二,改變特定:

[self.tabBar setBackgroundImage:[UIImage imageNamed:@"GuideImage1"]];
[self.tabBar setSelectionIndicatorImage:[UIImage imageNamed:@"GuideImage2"]];

 

改變TabBarItem的選擇與未選擇圖標

- (void)viewDidLoad {
    [super viewDidLoad];

    UIImage *carIcon = [UIImage imageNamed:@"CarIcon"];
    UIImage *grayCarIcon = [UIImage imageNamed:@"GrayCarIcon"];

    
    ViewController *vc1 = [[ViewController alloc] init];
    UITabBarItem *item1 = [[UITabBarItem alloc] initWithTitle:@"欄目A" image:carIcon tag:0];
    item1.selectedImage = grayCarIcon;
    vc1.tabBarItem = item1;
    
    
    UserGuideViewController *vc2 = [[UserGuideViewController alloc] init];
    UITabBarItem *item2 = [[UITabBarItem alloc] initWithTitle:@"欄目B" image:carIcon tag:0];
    item2.selectedImage = grayCarIcon;
    vc2.tabBarItem = item2;
    
    self.viewControllers = [[NSArray alloc] initWithObjects:vc1, vc2, nil];
    
    [self setSelectedViewController:vc2];
}

 

改變TabBarItem的文本顏色

UITabBarItem *item = self.tabBar.items[0];

NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:[UIColor grayColor],UITextAttributeTextColor, nil];
[item setTitleTextAttributes:dict forState:UIControlStateNormal];

NSDictionary *dict2 = [NSDictionary dictionaryWithObjectsAndKeys:[UIColor redColor],
                       UITextAttributeTextColor,nil];
[item setTitleTextAttributes:dict2 forState:UIControlStateSelected];

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM