UITabBarController

来源:互联网 发布:centos系统指令 编辑:程序博客网 时间:2024/05/16 12:34

这里写图片描述
//设置tabBar的颜色
tbc.tabBar.barTintColor = [UIColor whiteColor];
//设置选中时的颜色
tbc.tabBar.tintColor = [UIColor greenColor];
//设置tabBar的背景图片
tbc.tabBar.backgroundImage = [UIImage imageNamed:@”tabbar_bg”];

//定制tabbaritem
UITabBarItem *tbi = [[UITabBarItem alloc] initWithTitle:@”微信” image:image selectedImage:selectedImage];
//设置字体及颜色
[tbi setTitleTextAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:15], NSForegroundColorAttributeName:normalColor} forState:UIControlStateNormal];
[tbi setTitleTextAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:15],NSForegroundColorAttributeName:selectedColor} forState:UIControlStateSelected];
//自定制tabbar需隐藏系统tabbar
//首先隐藏系统tabBar
self.tabBar.hidden = YES;//self为tabbarcontroller
“`
//设置代理
tbc.delegate = self;

//读取以保存在沙盘里的选中下标
tbc.selectedIndex = [[NSUserDefaults standardUserDefaults] integerForKey:@”selectedIndex”];

//说明:下面代理方法是UITabBarControllerDelegate选中tabbaritem时代理方法,可以把选中时的下表保存到本地沙盘里

pragma mark - 代理方法

  • (void)tabBarController:(UITabBarController )tabBarController didSelectViewController:(UIViewController )viewController
    {
    //获取选中下标
    NSUInteger selectedIndex = tabBarController.selectedIndex;
    NSUserDefaults *ud = [NSUserDefaults standardUserDefaults];
    //会保存到沙盒目录,一般只保存小数据(状态记录等)
    [ud setInteger:selectedIndex forKey:@”selectedIndex”];
    //上面写不会立即写进入,下面的语句可以立即写入
    [ud synchronize];

    //打印沙盒目录
    //NSLog(@”%@”, NSHomeDirectory());
    }

“`

0 0